Difference between abstract class and interface


Abstract Class Interface
Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods.
Abstract class can have constructor. Interface can not have constructor.
Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
Calendar class is example of abstract class. ActionListener is example of interface.
Example:
 public abstract class Shape{
  public abstract void draw();
 }
Example:
 public interface Drawable{
  void draw();
 }

Example of abstract class and interface in Java

 // abstract class
 
 abstract class A{
  abstract void display();
 }

 // interface
 interface C{
  
  void test();
  void disp();
  
 }

 // class B that extends A and implements C
 
 class B extends A implements C{
  public static void main(String... str){
   B b=new B();
   
   // call interface methods
   
   b.disp();
   b.test();
   
   // call abstract class method
   
   b.display();
  }
  
  public void test(){
   System.out.println("test method.");
  }
  
  public void disp(){
   System.out.println("Disp method.");
  }
  public void display(){
   System.out.println("Display method.");
  }
 }

Share this

Related Posts

First