Abstract Classes VS Interface

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 513
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 89
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Abstract Classes VS Interface

 
0
  #1
Jul 30th, 2008
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?
KSG
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,503
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 521
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Abstract Classes VS Interface

 
1
  #2
Jul 30th, 2008
Originally Posted by selvaganapathy View Post
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.

Originally Posted by selvaganapathy View Post
Can abstract class replaces Interface?
In some cases, yes, in others, no. See next comment below.

Originally Posted by selvaganapathy View Post
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 513
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 89
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: Abstract Classes VS Interface

 
0
  #3
Jul 31st, 2008
Thanks for your information. Now i have clear idea about Abstract Class and Interface.

Thanks again
KSG
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC