• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

why interface

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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")
}
}
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interface is java answer to multiple inheritance......
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interface is java answer to multiple inheritance......
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic