File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Looking for a better understanding of interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Looking for a better understanding of interfaces" Watch "Looking for a better understanding of interfaces" New topic
Author

Looking for a better understanding of interfaces

Alex Birmingham
Ranch Hand

Joined: May 22, 2008
Posts: 54
Hey all,

So I just finished Head First Java and I'm looking to patch up a couple of misconceptions I'm having. First off, interfaces: how in the world are these useful at all?

It seems to me that going to the trouble of writing an interface and then implementing it in your classes is nothing more than a way of annotating said classes with an additional IS-A marker. EX: Wolf isn't just a Canine, he is also a PredatoryCreature.

But is that really all that helpful, when the methods contained within PredatoryCreature can't contain a body? Sure, they remind you to make sure that Wolf isn't just going to eat(), drink(), and sleep(), that Wolf needs to override methods like attack(), and growl(). but surely there is more to interfaces than a compile-time reminder to override some methods?

Unless their whole usefulness lies in the fact that implementating PredatoryCreature means that I can pass Wolf and some other PredatoryCreature to a method that should only accept PredatoryCreatures? I guess that would be a practical application of adding another IS-A flag to a class....but I could also have just added a boolean value to all my dogs.

boolean predatory = true/false;


- SCJA 1.0
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

It's both. Not only is there the IS-A check that the Wolf is also a PredatoryCreature, but the interface also makes sure that its methods really are implemented.

Your dog could have the boolean flag set, but does that guarantee the attack() and growl() methods? I don't think so.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Looking for a better understanding of interfaces