Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract and Interface

 
Uzma Khan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plz. Could anybody tell me about the difference between Abstract and Interface Class and also i want to know what is the use of Abstract class?
 
shweta agarwal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abstract classes r used when u want to make sure that their is a method that the subclass has to inherit and it does not a means to provide multiple inheritence but interfaces r used for multiple inheritence.
also u can make a reference of interface but not of any abstract class.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the only real differences I see between abstract classes and interfaces are A) you have to implement an interface, where as an abstract class is extended and B) an abstract class can contain methods with implementations, where as an interface can not and C) you can only extend one class, but you can implement any number of interfaces. While interfaces are Java's answer to multiple inheritance, it does not function quite the same way.
I can have the following code:
And I'll get "Foo!" printed out. I get the exact same response if I have this code:
The two abstract and interface classes act the same way here. However, I could add another method to the abstract Foo class, and give it an implementation, but could not do the same to the interface Foo, the compiler would give an error. Preferably, I would use the interface version over the abstract, since you only get to extend one class. The only time you would want to use abstract (correct me if I am wrong here) classes is if that class also needed some of its own implementation, or just to make the design make sense (an abstract Animal class probably wouldn't be a logical interface).
Hope that helps
Jason
[This message has been edited by jason adam (edited November 01, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic