Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes why interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why interface" Watch "why interface" New topic
Author

why interface

jagan chary
Greenhorn

Joined: Sep 24, 2007
Posts: 2
hello this is jagan from hyderabad
my doubt is why do we need interfaces we are not writing any implemention in interface methods

for ex:

interface Bounce
{
public void jump();
}

class Ball implements Bounce
{
public void jump()
{
System.out.println("ball jump")
}
}

class Tyre implements Bounce
{
public void jump()
{
System.out.println("tyre jump")
}
}

in above example we need provide behavior for the Ball (insted of implementing bounce interface why do't we just write new jump()
like below

class Ball {
public void jump()
{
System.out.println("ball jump")
}
}

class Tyre
{
public void jump()
{
System.out.println("tyre jump")
}
}
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rory Lynch
Ranch Hand

Joined: Aug 03, 2007
Posts: 95
In java you can only have single inheritance, so any class can only exend one class and therefore inherit form it.

So you could have a super class called Ball with subclasses of BeachBall, SnookerBall, SquashBall, TennisBall, which all inherit methods from Ball but specialize some of thos methods.

You could also have a Super class called Tyre and subclass it with MotorBikeTyre, SnowTyre, AirplaneTyre.

Now where should you put a method that you want both Tyres and Balls to have.

The answer is in an Interface and then make Ball and Tyre implement the interface. Now if an Orang-Utan plays with either a Tyre or a ball he can be guarantee they will be Bounceable.


I wish that for just one time, you could stand inside my shoes.<br />You'd know what a drag it is to see you.
Srikanth Iyer
Ranch Hand

Joined: Apr 30, 2007
Posts: 52
Interface is java answer to multiple inheritance......
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
IMHO Interfaces are a match nicer alternative to multiple inheritance.

Interfaces provide away to define very general behavior that is applicable to more then one group of objects. This allows you to define exactly how a given Class will implement that behaviour, either at a supper class level or at an indervidual subclass level.

Now you could do this as you defined, but it wouldnt be very polymorphic. You would end up with objects inheriting from Tyre having one set of behaviours and the ones inheriting from Ball with another, and all Ball's must override bounce, but BeanbagBall dosnt bounce

Interfaces on the other hand allow for polymorphic behaviour, in that you could have a collection of type Bounceable, and you would be able to just call the doBounce method on each member of the collection.
Where as without the Bounceable interface (just using inheritance) your collection of things you wish to test bounce will be of type Object, which dosnt have a doBounce method, so you would have to test each object with instanceof, and then cast to the appriate type.

Of course you could make Tyre and Ball have a common super class, perhaps called RubberItem this might be ok, until you need to give bounceable behaviour to something that isint a RubberItem, such as a Spring, should a Spring inherit from a rubberItem?

Interfaces can also be used as "markers", in that you do NOT provide any methods to be implemented, but your objects will still pass the IS-A test for the Interface.

Sorry if that is as clear as mud, just my thoughts on the subject.

G
Kamal
Greenhorn

Joined: Sep 18, 2007
Posts: 2
We use interfaces to define the behavior shared by non-related classes (i.e. classes that does not have is-a or has-a relationship). Whereas super class, abstract class will be used when the classes are related to each other.

As Rorry mentioned, the different ball types might be related to each other so as the tyres. But, if the tyres and ball need to share a common behavior, interface should be considerable.
Srikanth Iyer
Ranch Hand

Joined: Apr 30, 2007
Posts: 52
Interface is java answer to multiple inheritance......
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

interfaces don't give anything to the person implementing them. they help the person who is going to USE those classes.

a collection can only take a single type of object. I can therefore write my collection to accept Bounce objects. Now, it doesn't matter if that object is a Ball or a Tyre, i can stick them all in my collection, iterate through it, and call jump() on all the objects in there.

In fact, a year later, I can be on the beach sipping a drink when my boss calls. Our vendor is changing their "Tyre" class to a "Tire" class.

I don't care, because they will still be implementing the Bounce interface, so MY CODE WORKS.

Meanwhile, you, who wrote your code to expect a Tyre object, have to pack up your stuff, go into the office, and re-write your code to now accept a Tire object. you need to re-compile, re-test, and re-distribute your code to your 5000 customers all around the world.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Interface is Java's answer to multiple inheritance

I've seen this written a zillion times, but ... did Sun say that?

Implementing interfaces doesn't feel like inheritance to me. I don't inherit any behavior, I only take on responsibility to provide behavior.


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
 
jQuery in Action, 2nd edition
 
subject: why interface
 
Similar Threads
Polymorphic behavior provided by inheritance programming assignment
Using Threads
interface error
Passing an interface reference to a method expecting an "interface implementor"
An interface does not have to be implemented?