This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between interface and abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between interface and abstract class" Watch "Difference between interface and abstract class" New topic

Difference between interface and abstract class

Rahul Mayane

Joined: Aug 30, 2002
Posts: 5
what is the difference between interface and abstrct classes?
If interface can be used instead of abstract classes,why abstract classes are provided in java?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
An abstract class is like any other class except that at least one of its methods is declared abstract to indicate that it is not implemented in the class. With that in mind, if you compare a normal class with an interface you will see a vast difference between the two.
[ August 31, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the ranch Rahul.
Bruce Eckel recommends using an interface, instead of using an abstract class, whenever some common behaviour in the base class isn't needed.
It's possible to simulate multiple inheritance with interfaces, but not with abstract classes.
A reason to make abstract a class that hasn't got abstract methods, is to make explicit that it cannot be instantiated.

SCJP2. Please Indent your code using UBB Code
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Barry is almost correct. Abstract classes do not have to have any abstract methods. The Java API has several abstract classes without abstract methods. You make a class abstract when instantiating it doesn't make any sense but instantiating a child of the class does make sense.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
An abstract class without at least one abstract method :roll: ! I thought you were pulling my leg, but you are correct.

does in fact compile.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Take it to other extreeme. What if all methods of an abstract class are abstract. Is it now as good as an interface? Not quite so. I can think of following differences:
1. all class level variables in an interface are automatically final and static but in case of abstract class, you will have to explicitly declare them static and final. But that gives you some flexibility i.e. you can have some static, final var and others non-static, non-final at that level of abstractation.
2. any implementing class will be limited to this (abstract) class as a base class. Alternatively, if these abstract methods are implemented in an interface, the implementing class can draw from more than one class and still have opportunity to extend another class.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The MouseAdapter class from the java.awt.event package is an example of an abstract class with no abstract methods:
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Also, see this Javaworld article
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The MouseAdapter is a good example, Thomas, I never noticed that it was abstract; I just remembered that it had methods to be overridden. When meeting java's abstract modifier during an introduction to java, I took note of the fact that if a method is declared as abstract then the class had to be declared abstract. That seemed to be a bit redundant at the time. Now it makes a little more sense.
[ September 01, 2002: Message edited by: Barry Gaunt ]
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Here is a recent interesting discussion about the same topic:

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: Difference between interface and abstract class
Similar Threads
abstract class Vs interface
i want answer
Interface and Abstract
Abstract class and interface
Difference between abstract classes and interfaces