This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes abstract class and interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "abstract class and interface" Watch "abstract class and interface" New topic
Author

abstract class and interface

Suri Babs
Ranch Hand

Joined: Mar 27, 2003
Posts: 50
What is the difference between abstract class and interface? which is better to use and why?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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 ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
aadhi agathi
Ranch Hand

Joined: Apr 29, 2002
Posts: 263
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


Aadhi
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
So Joel, is this a Java lesson or a zoology lesson? All kidding aside, good explaination.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Suri Babs
Ranch Hand

Joined: Mar 27, 2003
Posts: 50
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

Joined: Mar 27, 2003
Posts: 50
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

Joined: Aug 20, 2001
Posts: 1817

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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract class and interface