Java "interface" vs. OO interface

From: Thomas G. Marshall (tgm2tothe10thpower_at_hotmail.replaceTextWithNumber.com)
Date: 11/13/03


Date: Thu, 13 Nov 2003 19:46:28 GMT


This is a beginner question for the OO purists in this group, because I've
often wondered if java made a mistake here (let's not let this descend into
a stack of all of java's mistakes).

For this discussion "interface" will mean "java's interface", and
OO-interface will mean the general OO term /interface/.

Java has a facility called literally the "interface", which as expected
allows me to establish part of an object's contract:

    public interface ChildInterface
    {
        public void runs(int reason, int speed);
        public void sleeps(int seconds);
    }

I could then /implement/ this interface as such:

    public class DwightSchmidlap implements ChildInterface
    {
        public void runs(int reason, int speed)
        { ....... }

        public void sleeps(int seconds)
        { ....... }

        public void somethingelse() // not in interface
        { ....... }
    }

And use the interface in variable /declarations/ where the actual
implementing classes are used in the variable /definition/:

    ChildInterface[] kids = {new DwightSchmidlap(), new Henry(), ...}

...if DwightSchmidlap and Henry both implemented the interface. Because of
/kids/ declaration I'm only able to access the members of the interface:

    kids[0].runs(....)
    kids[0].sleeps(...)

and not:

    kids[0].somethingelse(); // error. In underlying class, but not
declaration.

Fairly trivial. This is nothing you wouldn't expect.

Now here's my complaint, and perhaps it's ok with you.

The java interface allows me to specify constants in that interface.

    public interface ChildInterface
    {
        public static final int REASON_SCARED = 0;
        public static final int REASON_HAPPY = 1;
        public void runs(int reason, int speed);
        public void sleeps(int seconds);
    }

...in this case to provide potential values for the runs() method.

There are other things allowable in interfaces, which I'm not going into
here, I want to keep this focused.

The problem I have with this approach is that it allows fully UNqualified
access to any of the constants names anywhere downwind of the interface
implementation.

    public class MyClass extends SomeOtherClassThatImplementedChildInterface
    {
        public void bang()
        {
            runs(REASON_SCARED, 5.0);
            laterReason = REASON_HAPPY;
        }
        public int laterReason;
    }

Fully unqualified! (Yuck). This can (AND DOES) routinely cause headaches
as people throw time away trying to figure out where things are defined,
especially if the interface is implemented near the top of a large
hierarchy.

Question: Even if java /required/ a qualified access to reach the constants:

    runs(ChildInterface.REASON_SCARED, 5.0);

does it violate the notion of the OO-interface to have the java interface
allow constants within it? And if not, is that so either way: constants
related to the contract of the class and not related at all?

Thanks,

Thomas



Relevant Pages

  • Re: Darkroom software Help
    ... Apple's computer operating system OS-X includes the Java ... programing language, compiler, and integration software. ... have an Apple this fall. ... Said that, my system is based on a commercial lab interface, the ...
    (rec.photo.darkroom)
  • Re: A C++ Whishlist
    ... with the C++ string classes as compared with the Java ones. ... You can keep you structs entirely ... The simple separation of interface and implementation that header files ...
    (comp.lang.cpp)
  • Stuff the purple heart programmers cook up
    ... C To act like a framework for the JDBC driver developers. ... D To hide the specifics of accessing particular kinds of database ... Topic: Java 2: Survey Author: Chris Mc Devitt ... The JDBC ResultSet is actually an interface java.sql.ResultSet. ...
    (comp.lang.java.programmer)
  • Download Java / J2EE Interview questions
    ... JAVA Interview Questions ... What's an interface and how will you go about implementing an ... XML and Web Services ... Twist:- Explain in short all types of diagrams in UML? ...
    (comp.lang.java.programmer)
  • Download Java / J2EE Interview questions
    ... JAVA Interview Questions ... What's an interface and how will you go about implementing an ... XML and Web Services ... Twist:- Explain in short all types of diagrams in UML? ...
    (comp.lang.java.help)