It's not a secret anymore!
The moose likes Beginning Java and the fly likes What the??? Interface & Implements in regards to inheritance & polymorphisms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What the??? Interface & Implements in regards to inheritance & polymorphisms" Watch "What the??? Interface & Implements in regards to inheritance & polymorphisms" New topic

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

Tim Dee

Joined: May 07, 2005
Posts: 22
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

Joined: Oct 02, 2003
Posts: 11955

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!!!

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

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.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

All together now.

I agree. Here's the link:
subject: What the??? Interface & Implements in regards to inheritance & polymorphisms
It's not a secret anymore!