Difference between Abstract Class and Interface in java ?

Abstraction:

  • Hiding of unnecessary data is known as Abstraction. To achieve abstraction in java side we use abstract class and Interface.

    Abstract class:

    Abstract class is a class which contain zero or more abstract method.

    Abstract method is a method which contain only method header, that is present in its subclass. It does not contain any method body. Abstract classes can have both abstract as well as non-abstract methods.

    syntax:

    abstract class A
    
    {
    
    //statements;
    
    }

     

    Steps involved in creating abstract class

    1.Creation of abstract class.

    2.Creation of one or more sub classes.

    3. Main method to access.

    Interface:

    Interface is a collection of method prototypers or method headers. Interfaces can be communicate with several sub or implemented classes.

    Syntax:

    upto jdk 1.7 version

    interface interfacename
    
    {
    
    constants;
    
    abstract methods;
    
    }

     

    upto jdk 1.8 version

    interface interfacename
    
    {
    
    constants;
    
    abstract methods;
    
    static methods;
    
    }

     

    upto jdk 1.9 version

    interface interfacename
    
    {
    
    constants;
    
    abstract methods;
    
    private methods;
    
    }

     

    Steps involved in working with interface:

    1.Creation of Interface.

    2.Writing of one or more implementation classes.

    3.Main method() to access the interface with sub class reference.

     

    The Similarities between Abstract class and Interface are:

    1.Interface and Abstract class both contain zero or more abstract methods.

    2.We cannot create object for both Abstract class and Interface but we pass the reference.

    3.We cannot declare Abstract class and Interface as Static or Final.

    4.Both Interface and Abstract class support Constants.

    5.Abstract methods are supported by both Interface and Abstract class.

    6.Both abstract class and interface cannot be instantiated using the`new operator.

    The difference between Abstract Class and Interface are as follows..

     abstract class and interface

    Interface Abstract Class
     1. We can declare interface as abstract type.  1.It does not support interface modifiers.
     2.Interface does not support Variables.  2.Abstract Class support Variables.
     3.Interface support Constants by default treated as public static final type.  3.Abstract class also support constants must begin by final keyword.
     4.Static methods supporting Interface since jdk 1.8 version.  4.Static methods supporting Abstract class since jdk 1.0 version.
     5.By default Interface methods are public abstract type.  5.By default abstract class methods are default type.
     6.Interface method does not begin by abstract keyword.  6.Abstract method must begin by abstract keyword.
     7.Concrete methods are not supported.  7.Concrete methods are supported.
     8.Interface are fully secured.  8. Abstract class are partially secured.
     9.Default methods are supported in interface.  9.Default methods are not supported in Abstract class.
     10.Constructors are not supported.  10.Constructors are supported.
     11. We can create one interface and write ‘n’ number of implementation classes.In implementation classes, we use implement keyword.  11. We can create one Abstract class and write ‘n’ number of sub classes. In the sub classes, we use extend keywords.
     12.If no features are common then interface is recommanded.  12. If some features are common, or not common then abstract class is recommanded.
     13.From multiple interfaces, we can create one subclass.  13. From multiple Abstract classes, we cannot create one subclass.
     14.If programmer creates an Interface, that programmer or third party vendor write implementation classes.  14.If programmer creates an Abstract class, then same programmer has to write sub classes.
     15.Example:

    public abstract class Circle

    {
    public abstract void draw();
    }

     15. Example:

    public interface Circle

    {

    void draw();

    }

Read More:

difference between Class and Object in java?

Leave a Comment