Re: public interface method I'd rather not be

From: xarax (xarax_at_email.com)
Date: 08/11/04


Date: Wed, 11 Aug 2004 13:16:31 GMT


"VisionSet" <spam@ntlworld.com> wrote in message
news:toaSc.500$Qs4.454@newsfe5-gui.ntli.net...
>
> If I use an interface internally for my application, to aid future
> enhancement and maintainability.
> All the methods will still be public.
>
> I have a validation method which ordinarilly would be private.
> It simply does nothing if validation succeeds or throws exceptions
> otherwise.
> Not a typical public method.
>
> From a javadocs point of view this method will be right out there with the
> rest of the public interface
>
> I notice Sun has statements like this for some undesirably exposed methods:
>
> "This method is public as an implementation side effect. do not call or
> override."
>
> Is this good practise?
> What are my options?

If you must use an interface, then this is an unfortunate
side-effect of the language design.

With careful design, you may be able to use an abstract
class instead of an interface. (Remember that an abstract
class can be extended by an inner class, which gives the
inner class access to the outer class members and can be
referenced by the abstract class type. Use a factory
method that returns the abstract class type when it creates
the inner class.)

public abstract class FubarAbstract
{
    protected abstract void validate();

    public abstract void doSomethingPublic();
}

public class Snafu
{
    protected class FubarDefault
    extends FubarAbstract
    {
        protected void validate()
        {
            /* perform validation */
        }

        public void doSomethingPublic()
        {
            validate();
            /* do something useful */
        }
    }

    public FubarAbstract factoryFubarAbstract()
    {
        return new FubarDefault();
    }
}

public class Test
{
    public static void main(final String[] args)
    {
        Snafu snafu;
        FubarAbstract fa;

        snafu = new Snafu();
        fa = snafu.factoryFubarAbstract();
        fa.validate(); /* <== compile error; validate() is protected */
        fa.doSomethingPublic(); /* ok */
    }
}

-- 
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!


Relevant Pages

  • Re: Declaring a Constructor in an Interface?
    ... i have read many places that an interface is faster than ... but would You also claim that an abstract class ... Dennis JD Myrén ... > Virtual methods always means performance overhead. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Java/J2EE Openings in RTP, NC
    ... JSP, EJB, DAO Developer ... An abstract class is declared with the keyword "class" and is an implementation, i.e., its methods can contain bodies. ... An interface is a declaration of public method signatures which taken together represent a type with a defined contract for interaction with other types. ... A "Type 1" JDBC driver is a bridge to an ODBC driver which in turn interacts with the data store. ...
    (comp.lang.java.programmer)
  • Re: Abstract class or interface?
    ... >derived classes can only inherit one abstract class. ... The interface properties/methods have no implementation. ... and rotate "things" along all three X, Y, and Z coordinate axis. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Abstract class or interface?
    ... I try to make the decision based on the relationship of the derived class to the base class. ... If the derived class "can act like" the type of the base class, I'd lean towards an interface. ... I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using abstract class that implements interface
    ... colleges - that come from JAVA programming - says - it is redundant ... and interface is unnecessary... ... abstract class is in whether all instances must be required to share the ... The design permits future implementations that do not share the same base ...
    (comp.lang.java.programmer)