• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class and interface

 
Suri Babs
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between abstract class and interface? which is better to use and why?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An abstract class is any class that is declared as "abstract"; this means that you cannot create an instance of that class, you must create an instance of a (concrete) subclass. Typically, although not all the time, an abstract class will have abstract methods--that is, methods that have a declaration but no implementation. These methods must be implemented in a subclass, or else that subclass must be declared as abstract as well and the methods must be implemented in some subclass of the subclass.
An interface is (at a basic level) an abstract class with no methods defined. In addition, it can only define member variables that are constant (final).
You would use an abstract class when you wanted to provide base functionality but require some specific details in some methods. An example would be a Vertebratum class. This class ensures that all animals in the class have a backbone, but you cannot actually instantiate a Vertebratum object. You would have to instantiate a HomoSapiens (Human) or a CanisDomesticus (Dog) subclass.
use an interface when you think that a large number of different things can perform similar functionality. An example would be a Bird and a Bat class. Bird is a subclass of Vertebratum, while Bat is a subclass of Mammalia (which is a subclass of Vertebratum). However, both Birds and Bats can fly, so they both implement the Flyer interface (along with a number of Insects)
(Note that in this example, Bird and Bat would both be abstract classes; also, the Penguin subclass of bird would throw some sort of exception (most likely java.lang.UnsupportedOperationException) when you asked it to fly, although it would implement the Swimmer interface)
Hope that this helps.
[ July 21, 2003: Message edited by: Joel McNary ]
 
aadhi agathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract class provides some default implementation and dictates the flow in addition to services. Whereas interface provides only the service defintion.
There is nothing like superiority between these two.have a look
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Joel, is this a Java lesson or a zoology lesson? All kidding aside, good explaination.
 
Suri Babs
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Morris:
So Joel, is this a Java lesson or a zoology lesson? All kidding aside, good explaination.
 
Suri Babs
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Morris:
So Joel, is this a Java lesson or a zoology lesson? All kidding aside, good explaination.


Every thing thats there in OOPs is based on real time objects, their properties and behaviour. So no kidding.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Morris:
So Joel, is this a Java lesson or a zoology lesson? All kidding aside, good explaination.

Thanks. I use this quite a lot to explain OO concepts. My spine is private, so it is encapsulated (Chiropracters have to use the access methods to work with it)... The inheritance/interfaces are listed above... and my favourite is polymorphism, because the "reproduce" method in Mammal ("Give bith to live young") is overridden by the Platypus subclass ("lay eggs"). And, using C++, Surgeons can be declared friends so that they have direct access to my spine/heart/etc.
I sent my wife into fits of laughter when I kept stumbling over the phrase "Pregnant Platypus" and finally substituted "Knocked-up Monotreme."
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be completely accurate, an abstract class is any class that has the word "abstract" in its class defintion. It does not have to have any abstract methods. (Examples can be found in the adapter classes in the AWT.) Or it can have all abstract methods.
The rule is that any class that has an abstract method MUST be defined as abstract but an abstract class does not have to have abstract methods.
Abstract classes count towards your one inheritance.
Interfaces can only have abstract methods. In fact they can only have public abstract methods. Any variables defined in them must be public, static, and final. They do not count towards your one inheritance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic