Java "interface" vs. OO interface
From: Thomas G. Marshall (tgm2tothe10thpower_at_hotmail.replaceTextWithNumber.com)
Date: 11/13/03
- Next message: Myles: "Re: Programming Language Productivity: The Stupidity of Programmers"
- Previous message: CHANGE DOMAIN TO SECURESOFTWARE.COM TO EMAIL ME: "VMS (was: GUI v Script)"
- Next in thread: Costin Cozianu: "Re: Java "interface" vs. OO interface"
- Reply: Costin Cozianu: "Re: Java "interface" vs. OO interface"
- Reply: Rod Davison: "Re: Java "interface" vs. OO interface"
- Reply: Tsolak Petrosian: "Re: Java "interface" vs. OO interface"
- Reply: NFish: "Re: Java "interface" vs. OO interface"
- Reply: Ilja Preuß: "Re: Java "interface" vs. OO interface"
- Reply: Doc O'Leary: "Re: Java "interface" vs. OO interface"
- Reply: Thomas G. Marshall: "SORRY RED HERRING: 5.0 is 5 Re: Java "interface" vs. OO interface"
- Reply: H. S. Lahman: "Re: Java "interface" vs. OO interface"
- Reply: Dave Harris: "Re: Java "interface" vs. OO interface"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Myles: "Re: Programming Language Productivity: The Stupidity of Programmers"
- Previous message: CHANGE DOMAIN TO SECURESOFTWARE.COM TO EMAIL ME: "VMS (was: GUI v Script)"
- Next in thread: Costin Cozianu: "Re: Java "interface" vs. OO interface"
- Reply: Costin Cozianu: "Re: Java "interface" vs. OO interface"
- Reply: Rod Davison: "Re: Java "interface" vs. OO interface"
- Reply: Tsolak Petrosian: "Re: Java "interface" vs. OO interface"
- Reply: NFish: "Re: Java "interface" vs. OO interface"
- Reply: Ilja Preuß: "Re: Java "interface" vs. OO interface"
- Reply: Doc O'Leary: "Re: Java "interface" vs. OO interface"
- Reply: Thomas G. Marshall: "SORRY RED HERRING: 5.0 is 5 Re: Java "interface" vs. OO interface"
- Reply: H. S. Lahman: "Re: Java "interface" vs. OO interface"
- Reply: Dave Harris: "Re: Java "interface" vs. OO interface"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|