Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What the??? Interface & Implements in regards to inheritance & polymorphisms

 
Tim Dee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a hard time understanding Interface & Implements in regards to inheritance & polymorphisms.

I have enjoyed reading Java Head First up to chapter 8 where it totally lost me. Perhaps reading another explanation will help me get my head around it.

Many thanks for any help. It's much appreciated.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface is basically a list of methods (with their arguments - the signiature). You are stating in the interface what an object must do in order to be considered that.

in other words, if i have


then anything that wants to be a Comedian has to implement these three functions.

So, when i make a class


the 'implements Comedian' says to the compiler "i promise that this class PropComic will have every method defined in the comedian class". if it doesn't the compiler complains.

i can make another class, ObservationalHumorist, who also implements the Comedian interface. or even a talkShowHost class that implements it.

what good does this do me? i can make a collection of Comedians. some might be propComics, some might be talkShowHosts, or whatever. but since all implement the interface, i can treat them all as Comedians. i KNOW i can call the tellJoke() method on all of them, because they are GARANTEED to have that method defined.

Now, a class can implement as many interfaces as it wants/needs to. So the class can be treated as ANY of those types (as well as whatever it extends).

hope that helps, but if not, just ask more questions!!!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it is like this. When you implement an interface, it is not exactly inheritance. In Inheritance you extend another Object and therefore get all the methods and variables that are not declared private automatically in the new class.

With polymorphism, since the implementation classes all implement the same interface, they have the same methods available, so that a calling object does not need to know the actual implementing class, it just uses the methods of the interface

Here is a simple example



In the last class I could make var point to either a Class1 or a Class2, it doesn't matter since var is just a MyInterface, and not specific to Class1 or Class2.

No I could create another class that has a method that returns either Class1 or Class2, the signature just needs to show that it returns a MyInterface.

Here is that example.



These are examples of polymorphism.

Mark
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implementing an interface and extending (inheriting) a base class are very similar but subtly different ways to achive polymorphism.

Say you make a method that takes a List argument

If we look in the JavaDoc we see List is an interface. You know that any object that comes into your method has the methods promised by the List interface, but you don't have to know any more than that. Ignoring certain details so you can concentrate on the important bits - the argument implements List - is the heart of "abstraction".

Now let's look at a method that requires a base class:

If we look at the doc we find Observable is a class, not an interface. Again we know that the argument passed has all the methods and fields promised by Observable. Further we know that the argument extends Observable. This is more knowlege, less abstract.

Both ways we get polymorphism - callers can pass us objects of classes that we've never heard of before, we can call the methods we have been promised, and those methods can do different things appropriate to the different classes. Interface is a little more abstract which is usually A Good Thing for us. With extends the caller gets some behavior from the base class which may also be A Good Thing for him.

Is that the right kind of discussion?
[ May 31, 2005: Message edited by: Stan James ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic