954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Abstract Classes VS Interface

Can any one tell about the difference between the Abstract Class and Interface? Can abstract class replaces Interface? If all things can be done in Abstract class Why we need Interface?

selvaganapathy
Posting Pro
547 posts since Feb 2008
Reputation Points: 44
Solved Threads: 100
 
Can any one tell about the difference between the Abstract Class and Interface?


Abstract classes can contain implementations of some methods, while interfaces cannot. This allows abstract classes to serve as a base implementation of a class hierarchy that shares some (or much) common code and require that subclasses implement only the portions that are specific to themselves.Can abstract class replaces Interface?
In some cases, yes, in others, no. See next comment below.If all things can be done in Abstract class Why we need Interface?Java only allows for single inheritance of classes, but the number of interfaces implemented is unlimited. Use of interfaces allows classes to act as more than one type of object. Without interfaces, all methods that you may want a group of classes to share must be stuffed into a common base class. What if some of these methods only apply to some classes but not others? Your class hierarchy would become a polluted mess, littered with methods that may not even apply to a particular class. Interfaces operate as a contract that guarantees a class implements one or more methods without forcing them to descend from a common base class. Many different kinds of class hierarchies may want to be Printable, or Comparable, or Displayable, without having to extend from some common ancestor. That can only be accomplished with interfaces.

You read more about this here if you wish: http://www.codeguru.com/java/tij/tij0080.shtml

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Thanks for your information. Now i have clear idea about Abstract Class and Interface.

Thanks again

selvaganapathy
Posting Pro
547 posts since Feb 2008
Reputation Points: 44
Solved Threads: 100
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You