Difference Between Abstract Class and Interface

Difference Between Abstract Class and Interface


In Java both abstract class and interface are used to achieve abstraction in which we can declare abstract methods. Both abstract class and interface cannot be instantiated. The following are some of the differences between the two:

Abstract Class Interface
It can consist abstract methods and non abstract methods also. Interface can only have abstract methods.
Multiple inheritance is not supported by abstract class. Multiple inheritance is supported by abstract class.
Static, non static, final, non final variables are supported by abstract class. Only static and final variables are supported by abstract class.
Main method and static method even constructors are supported by abstract class. Main method and static method even constructors are not supported by abstract class.
Implementation of interface can be done in an abstract class. Implementation of abstract class cannot be done in an interface.
Abstract class is declared by using the abstract keyword. Interface is declared by using the interface keyword.

Consider the following example in which there is an abstract class and interface:

CODE:

interface B {

void x ();

void y ();

void z ();

}

abstract class C implements B {

public void x () {

system. out. println (“Method x”);

}

}

class D extends C {

public void y () {

system. out. prinln (“Method y”);

}

public void z () {

system. out. println (“Method z”);

}

}

class ABC {

public static void main (string args []) {

B b1 = new D ();

b1. x ();

b1. y ();

b1. z ();

}

}

OUTPUT:

Method x

Method y

Method z