• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Have doubt in Interface.

 
Praveen Kumar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

My question is on concept of Abstract class and interfaces.

At Abstract class , we can have abstract methods and concrete methods too(optional). Where as Interface has to have all abstract methods.

Here at abstract class itself we can have all abstract methods.... then what is need of having interface in java.

Please don't reminder me Java not supports Multiple Inheritance.

There is some concept is why really interfaces is introduced.

Please help.

Praveen.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Praveen Kumar:

Please don't reminder me Java not supports Multiple Inheritance.


Well, OK, but then I can't tell you why Java has interfaces. You have to pick one or the other requirement here!
 
John Griffin
author
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praveen,

One thing that an Interface can do for you is provide a standard method signature for a callback by the compiler. For example, let's say that some library you are using provides some type of data and provides this data via a call to a Printable Interface that the library tells you to implement on any class where you want to print out the data.

Since it will just be calling the print method of the class and providing the data it has no idea of the implementation you provide. You can print to a file, to a socket, to a screen, anywhere. The point is that the application library just calls the method of the class implementing the interface without regrad to the implementation.

Hope this helps.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure that does help, John, and welcome to JavaRanch

But think how much more we could help if we were allowed to say "mimics multiple inheritance."
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:

Well, OK, but then I can't tell you why Java has interfaces. You have to pick one or the other requirement here!


Mhh, I wonder wether just providing a pointer to a site that does it qualifies...

http://faq.javaranch.com/java/InterfaceVsAbstractClass
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic