How do you differentiate abstract class from interface?

How do you differentiate abstract class from interface?

Asked on November 13, 2018 in Java.
Add Comment

  • 2 Answer(s)
    I don’t want to highlight the differences, which have been already said in many answers ( regarding public static final modifiers for variables in interface & support for protected, private methods in abstract classes)

    In simple terms, I would like to say:

    interface: To implement a contract by multiple unrelated objects

    abstract class: To implement the same or different behaviour among multiple related objects

    From the Oracle documentation

    Consider using abstract classes if :

    1. You want to share code among several closely related classes.
    2. You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
    3. You want to declare non-static or non-final fields.

    Consider using interfaces if :

    1. You expect that unrelated classes would implement your interface. For example,many unrelated objects can implement Serializable interface.
    2. You want to specify the behaviour of a particular data type, but not concerned about who implements its behaviour.
    3. You want to take advantage of multiple inheritance of type.

    abstract class establishes “is a” relation with concrete classes. interface provides “has a” capability for classes.

    If you are looking for Java as programming language, here are a few more updates:

    Java 8 has reduced the gap between interface and abstract classes to some extent by providing a default method feature. An interface does not have an implementation for a method is no longer valid now.

    Answered on January 18, 2019.
    Add Comment

    Let’s see a simple example where we are using interface and abstract class both.




      1. //Creating interface that has 4 methods


      1. interface A{


      1. void a();//bydefault, public and abstract


      1. void b();


      1. void c();


      1. void d();


      1. }




      1. //Creating abstract class that provides the implementation of one method of A interface


      1. abstract class B implements A{


      1. public void c(){System.out.println(“I am C”);}


      1. }




      1. //Creating subclass of abstract class, now we need to provide the implementation of rest of the methods


      1. class M extends B{


      1. public void a(){System.out.println(“I am a”);}


      1. public void b(){System.out.println(“I am b”);}


      1. public void d(){System.out.println(“I am d”);}


      1. }




      1. //Creating a test class that calls the methods of A interface


      1. class Test5{


      1. public static void main(String args[]){


      1. A a=new M();


      1. a.a();


      1. a.b();


      1. a.c();


      1. a.d();


      1. }}







    Answered on January 18, 2019.
    Add Comment

  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.