- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
LordHunter wrote: "And when you consider that the only difference between an interface and a class is that the former can only declare methods, you realize the former definition is full encasuplated by the latter, as such, it's a touch foolish to define MI solely by the latter."
So, if a more generic definition exists for something, it is "foolish" to use a more specific one? I suppose that, too, is a matter of opinion.
Admin
In this case, Yes, it is foolish because the more specific definition doesn't cover all cases being discussed.
Admin
Well, seems to me you're no Java guru either. (Also, you should stick your tongue in your cheek more often, just to get a feeling for it.)
More likely the interface is just what it states to be, an interface. If your class implements an interface you sort of say "I do offer these operations, you can call me on that." That's it.
Doing nothing is NOT the default functionality for any registered Listener; if it was, why register it anyway? The Adapter exists solely for those cases you're not interested in all events this Listener recieves.
Yes, that's a lot clearer now. I thought you were refering to objects.
Yes. But that's still no object. That's your famous "pointer", or your "reference" in Javaspeak.
(Besides, that's one of the nifty thing about interfaces: you have, e.g., some interface modeling some container, and you can - unless you're a bad boy and cast all the time, in which case your design sucks anyway - exchange the actual type of the container without leafing to every single line of codes adjusting it. Or accept in your methods any container that implements that interface.)
I'm not really sure what you're talking about. You'll have some class anyway. And in fact, I find it more useful to have inner classes when the class is of absolutely no use to anything else outside of the class.
FYI, "Design-by-contract" is more about pre- and postconditions.
If you can't see the intent of the code, well... better not comment it. Or just wait - good idea, comment it, or make it a struct so you see straight away that's no ordinary class.
Admin
This shows you missed the point: the interface is a completely redundant construct. All of it's abilites are already covered by other language constructs.
So the question then becomes: why are interfaces important enough to get a special construct? And the answer is: because modeling behaviors on disjoint sets of data is useful. But that's the only reason, and it doesn't imply (or even suggest) that interfaces should be used where another construct (e.g., ABT or a plain class) is more appropriate.
And, unless you can come up with a case where the Adapter's default behavior is incorrect, I'd argue the interfaces should go away completely, because they are redundant. For the specific case of Java event-handling, I don't think such a case exists. This is the crux of my point.
Yes, and it's a reference to an object. If you want to follow that line of argument, then all I have in java are references and primitive types, it's impossible to have an object, ever. I'm afraid this reasoning is invalid.
Since interfaces are part of the class (object) hiearchy, they should be considered objects. If this were C++, we wouldn't even be having this discussion.
Or you're using Java or C# and such casts are foisted on you by the shitty API.
(emphasis mine). So what do we call doing something automatically without any input? That's right: a default. .
Admin
I know it sounds absurd, but there actually are conclusions that don't result from assertions of the opposite. Anyway, no offence meant.
Beg your pardon, but I dare to disagree. The interface is NOT completely redundant, especially since Java does not have - classwise - multiple inheritance. That's for all abilities covered by other language constructs, but it goes on: it adds an ability to an other language construct, namely class. Moreover, it's semantically different from class.
As for the WindowListener issue, it seems this time you missed my point. If you register an Listener you sort of state "I am interested in Window events". The adapter is only for cases you're not interested in all of them; it spares you a few lines of code. And no, deriving from WindowAdapter and implementing WindowListener is not the same, so there's no redundancy - if at all, the adapter is redundant, and surely wouldn't exist wasn't it such a commonly used utility.
I'm afraid to disappoint you, but this reasoning is everything but invalid.
For one thing, a reference to an object isn't the same as the object itself. For another, any object you're refering to, be it via class or interface, is an object of a concrete class; there's neither objects of abstract classes nor interfaces.
I'll wager we'd have, too, as classes and objects aren't exactly the very same thing.
If you're hinting at collections prior to Java 1.5 you didn't catch my drift.
Admin
Which is why all the interfaces exist in Java, even when they are really redudnant in reality.
As for practical matters, it turns not not having classwise multiple inheritance isn't a big deal because you tend not to implement a ton of interfaces in the same class anyway. You almost always use an inner class or an anoymous class, because it makes the code more modular and manageable (and worlds uglier [:S]). So even in practical terms, the interface construct even in Java could be done away with (not that I'm advocating it should be, but it certainly could be). The fallout: that inner classes must be used in cases where they wouldn't have to be before, doesn't seem like a huge problem to me because they were likely to be used anyway. If anything, it could be a plus, not a minus.
Right, meaning it's providing default functionality for every event you're not interested in. Which means by consequence, if this default functionality is correct for every case where the interface is used, the interface must be redudant. Remember, the only difference between the Listener and it's Adapter (ignoring pesky class v. interface inheritence rules) is the fact the Adapter has a bunch of implementations that do nothing and the Listener does not.
You're thinking about it backwards. The Adapter cannot be redundant; it is a more complete specification of the interface. The interface can be redundant if and only if all cases where it is used could be substituted by use of the Adapter instead. I'm suggesting that in the case of Java events, this is very much true; the more complete Adapter specification is fully interchangable for the less complete Listener specification (because everyone meets the extra requirements of the Adapter). Ignoring the interface vs. class stupidity, of course.
In Java it certainly is. Classes don't have value-type semantics. I can only access a class via a reference.
I don't believe that disqualifies calling an interface an "object type" (or construct). What you're saying is that the ability to instantiate is a requirement to call something an object type. I don't believe that's true, if it were, languages would use a different keyword for all abstract types. I think the only requirement to consider something an object type is that it must have the ability to be inherited from. Interfaces and ABCs certainly fit that description. Final classes do too, as they normally have that ability, but it's been explictly disabled (OK, they're a slight strech ;) ).
Once again, I maintain if this were C++ and not Java, we wouldn't even be having this discussion.
This is because C++'s definitions are funny with regard to the rest of the world.
No, I caught it, but even post Java 5, the language still foists the requirment to cast upon you. Java 5 just goes a long distance to minimize how often these casts must occur. But if WindowAdapter covers all cases where I'd use WindowListener, then it surely is.
Admin
My last post on this issue:
Classes are constructs which contain methods, delegates, properties, and variables. They must be instantiated to be used (except Const and Static members, of course).
In Java / C#, a given Class can directly INHERIT from exactly one CLASS. They can IMPLEMENT any number of interfaces. Therefore, INHERITANCE is SINGLE in that literal definition, which I think you'll find most people agree on.
Yes, you can achieve the same results as multiple inheritance by utilizing interfaces, but just because there is a workaround, it doesn't make it a language feature. I consider it to be much like other constructs that are present in non-Java languages, but have a "workaround" to accomplish similar results in Java.
Objects are instantiated constructs, and are not the same as classes, interfaces, etc.
Aside from you, I've never seen anyone try to say that Java or C# support multiple inheritance - they will all (yourself, myself included) agree that you can accomplish a similar result using interfaces, but they won't come out and say that these languages DO support it. I challenge you to provide a Credible Source(tm) of anyone making such a claim.
Admin
You know, I was just about to reply, but the forum software is still doesn't work properly and frankly I can't be bothered to dismantle your post. I'm getting the hunch you're just pulling my legs anyway, being deliberately obtuse.
Your first posts I could partly agree with (although that might've been just my bad english or your by times rather obscure way to express yourself) - but this last one, honestly, and still no offence meant (or congratulations, possibly, it's hilarious), is simply bollocks. And phenomenally so, too.
Admin
<FONT style="BACKGROUND-COLOR: #efefef">Even the MS docs say that .Net (exception of C++) does not support multiple inheritance. They mention multiple interface inheritance, and say its to avoid unpredicatable results. (Or "Unholy Messes" which is what I generally end up with ;) )</FONT>
<FONT style="BACKGROUND-COLOR: #efefef">At a low level (afaik) a referance to an interface is basically a pointer into a classes vftable with an offset and a *this (but you dont know what type the *this is).</FONT>
Admin
Actually Listeners are an implementation of the Observer-Observable pattern. Not a poor implementation choice at all, if you know what you are doing.
Making your public GUI component implement the listener is WRONG.
Making inner classes in your GUI class better but still WRONG.
The only things implementing these interfaces should be Controller classes. And don't get me started on extending JFrame to add components to it. Thats wrong too.
Of course if you don't want to actually understand what you are doing, you can just barf up some .NET.
Admin
the real WTF here is that Java encourages such design.
how could some poor programmer know better when the official API contains such mess.
Admin
Because there is a class is the AWT (WTF) library that does this, it means that Java encourages it? [8-)] I don't think so. In any event it actually kind of makes sense for that class. Of course, you'd have to understand what that class does first.
Admin
No.. it's 'knife []'
Admin
This an example of code that would be much more readable if Java would implement a <FONT color=#0000ff>does_not_implement</FONT> keyword.
[8-|]
Admin
Or a implements everything keyword =P
Admin
Let me guess what the method body looks like:
if(source instanceof Message){
Message m = (Message)source;
///blah blah
}else if(source instanceof Error){
Error e = (Error) source;
///blah blah
}else if(source instanceof HelpRequest){
....
you get the idea