difference between abstract class and interface

Posted on 2008-11-04
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
    LVL 16

    Accepted Solution

    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 59

    Assisted Solution

    by:Kevin Cross
    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 59

    Assisted Solution

    by:Kevin Cross
    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

    nice answers

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Title # Comments Views Activity
    strDist challenge 35 69
    mapBully challenge 6 53
    wordappend challenge 8 45
    solarwind tftp server 2 18
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    This video teaches viewers about errors in exception handling.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now