Sunday, 23 September 2018

Difference between Abstract Classes and Interface in Java

Abstract Class:

Abstract class can have abstract and non-abstract methods.

Abstract class doesn't support multiple inheritance.

Abstract class can have final, non-final, static & non-static variable.

Abstract class can have static methods, main method and constructors.

Abstract class can provide the implementation of interface.

    public abstract class Shape
          {
                public abstract void shape();
          }


 A Java abstract class can have instance methods that implements a default behavior.

 An abstract class may contain non-final variable.

An abstract class can extend another Java class & implement multiple Java interfaces.

Java abstract classes are faster than interface.

Interface: 

Interface can have only abstract method.

Interface support multiple inheritance.

Interface has only static & final variable.

Interface can't  have static methods main method or constructor.

Interface can't provide the implementation of abstract classes.

public interface Drawable {
 void draw();
}

Method of Java interface are implicitly abstract & cannot have implementation.

Java interface should be implemented using keyword "implements".

Java  class can implements multiple interface.

In comparison with  java abstract classes, java interface  are slow as it requires extra indirection. 

No comments:

Post a Comment

If you have any doubt please let me know

Methods in JAVA Language

 In general, a way may be thanks to perform some task. Similarly, the tactic in Java may be a collection of instructions that performs a sel...