difference between abstract class and interface

Posted on 2008-11-04
Medium Priority
Last Modified: 2010-04-21
what is the difference between abstract class and interface?
where to user abstract class and where to use interface, give real time example.?
Question by:Asheesh_Kumar
  • 2
LVL 16

Accepted Solution

Gyanendra Singh earned 400 total points
ID: 22876807
1. Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. An abstract class can contain no abstract methods also i.e. abstract class may contain concrete methods. A Java Interface can contain only method declarations and public static final constants and doesn't contain their implementation. The classes which implement the Interface must provide the method definition for all the methods present.

2. Abstract class definition begins with the keyword "abstract" keyword followed by Class definition. An Interface definition begins with the keyword "interface".

3. Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses. Interfaces are useful in a situation when all its properties need to be implemented by subclasses

4. All variables in an Interface are by default - public static final while an abstract class can have instance variables.

5. An interface is also used in situations when a class needs to extend an other class apart from the abstract class. In such situations its not possible to have multiple inheritance of classes. An interface on the other hand can be used when it is required to implement one or more interfaces. Abstract class does not support Multiple Inheritance whereas an Interface supports multiple Inheritance.

6. An Interface can only have public members whereas an abstract class can contain private as well as protected members.

7. A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class.

8. The problem with an interface is, if you want to add a new feature (method) in its contract, then you MUST implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass

9. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast

10.Interfaces are often used to describe the peripheral abilities of a class, and not its central identity, E.g. an Automobile class might
implement the Recyclable interface, which could apply to many otherwise totally unrelated objects.

Note: There is no difference between a fully abstract class (all methods declared as abstract and all fields are public static final) and an interface.

Note: If the various objects are all of-a-kind, and share a common state and behavior, then tend towards a common base class. If all they
share is a set of method signatures, then tend towards an interface.

Neither Abstract classes nor Interface can be instantiated.
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 600 total points
ID: 22876988
You may find this helpful, but will give some examples of usage below:

Interfaces lay the framework for what a Class's public capabilities are.  These are usually named by verbs (whereas Classes are usually nouns).  For example, an interface for a class called Animal could be Speaks with a public interface of speak().

public interface Speaks {
    public void speak();

class Animal implements Speaks {
     private String sound;

     public void speak() {

As you can see the Animal must implement the public interface method speak() and thus outside world can trust that if they instantiate an object of any type that implements Speak interface that they can then call speak method and know that it will be there.

Speaks thing = new Animal();

As you can see if allows for good object oriented programming practice as well as ensuring that in future updates you don't accidentally remove something that is being used by the outside world.

Interfaces cannot have any implementation, so Abstract classes are like methods in that they define methods that must be implemented by classes that inherit from it except you can have some concrete base code already defined to save the leg work later.

Now in this same example if we made Animal abstract, we could have a class Cat and a class Dog that both inherit from Animal but implement it in different ways as abstract classes just provide implementation framework.

abstract class Animal implements Speaks {
     protected String sound;

     public void speak() {
     void setSound(String s);

class Cat extends Animal {
     void setSound(String s) {
         sound = s;

class Dog extends Animal {
     void setSound(String s) {
         sound = s + " " + s;

Animal cat = new Cat();
Animal dog = new Dog();

// this is not necessary, just showing that this is still valid
Speaks speaker = cat;

Hopefully those help explain it.
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 600 total points
ID: 22877029
Took so long to type, I see BondinASP layed this out nicely above.  To follow-up on what was said, as you can see you can have an Abstract base class that implements an Interface so that you can write implementation code once and it is now usable by all the subclasses as BondinASP indicated.  As you will notice, it also defined additional methods that are not required to be public that must be implemented by any base clients that are not themselves Abstract.

Good luck!

Author Closing Comment

ID: 31513092
nice answers

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question