Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

diff b/w abstract class & interface

 
shailaja siddappa
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi pals,

can you tell me a difference between abstarct class and interface with proper real time example.


Thanks,
shailaja
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have a FAQ about this topic here.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well lets not get far away to get a practical use. Take java.util.Calendar for example. It is an abstract class. So it provides implementation for most of the methods leaving a few. The ones left are too specific for a generic calendar to implement. But java.util.Collection is an interface as everything related to it is too specific for a generic collection to implement...
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shialaja siddappa wrote:Hi pals,

can you tell me a difference between abstarct class and interface with proper real time example.


Thanks,
shailaja


Interfaces are used to add optional behaviour in class. Consider Comparable interface that allows a class to make sure that its instances are ordered with respect to other mutually comparable objects. Such interface is called a Mixin. You cannot define Abstract Classes as mixin.

 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces are used to add optional behaviour in class.


Not optional behavior. Having a class implement an interface means that you're saying that the class will perform behaviors (methods) specified by the interface.

John.
 
Christian Polychroniadis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have read all the reply's, but for me this is the explanation:

The basic difference is that an interface is like a 100% abstract class, that means ALL of his methods are abstract, ALL. But an Abstract class can have, non Abstract methods too, so you have an specific behaviour, this is one's of the differences, think about it, you are getting a more specific behaviour with an abstract class.

The other difference is that in JAVA you can not extend more than one class, if you make an abstract class, you have to extend it, and you can not extend any other class, you loose that posibility. But is different with an interface, you can implement one or more interfaces, and have the posibility to extend any other class, so is more flexible, depends on what you need.

I hope this could help you, Bye.

Christian Polychroniadis soon SJCP...
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John de Michele wrote:
Interfaces are used to add optional behaviour in class.


Not optional behavior. Having a class implement an interface means that you're saying that the class will perform behaviors (methods) specified by the interface.

John.


Ohh... you didn't understand my point. Let takes an example of Serialization. Its an optional behavior for any class to implement, but ones implemented, offcourse it becomes necessary for a class to implement all those methods defined in Interface.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic