File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Looking for a better understanding of interfaces

 
Alex Birmingham
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Rob Spoor
Sheriff
Pie
Posts: 20386
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic