aspose file tools*
The moose likes Beginning Java and the fly likes diff b/w abstract class & interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "diff b/w abstract class & interface" Watch "diff b/w abstract class & interface" New topic
Author

diff b/w abstract class & interface

shailaja siddappa
Greenhorn

Joined: Apr 14, 2009
Posts: 16
Hi pals,

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


Thanks,
shailaja
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

We have a FAQ about this topic here.


[My Blog]
All roads lead to JavaRanch
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

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.


http://muhammadkhojaye.blogspot.com/
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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

Joined: May 18, 2008
Posts: 8

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

Joined: Apr 12, 2009
Posts: 449

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: diff b/w abstract class & interface