Differences between abstract class and interface in Java

JavaObject Oriented ProgrammingProgramming

In Java, abstraction is achieved using Abstract classes and interfaces. Both contains abstract methods which a child class or implementing class has to implement. Following are the important differences between abstract class and an interface.

Sr. No.
Key
Abstract Class
Interface
1
Supported Methods
Abstract class can have both an abstract as well as concrete methods.
Interface can have only abstract methods. Java 8 onwards, it can have default as well as static methods.
2
Multiple Inheritance
Multiple Inheritance is not supported.
Interface supports Multiple Inheritance.
3
Supported Variables
final, non-final, static and non-static variables supported.
Only static and final variables are permitted.
4
Implementation
Abstract class can implement an interface.
Interface can not implement an interface, it can extend an interface.
5
Keyword
Abstract class declared using abstract keyword.
Interface is declared using interface keyword.
6
Inheritance
Abstract class can inherit another class using extends keyword and implement an interface.
Interface can inherit only an inteface.
7
Inheritance
Abstract class can be inherited using extends keyword.
Interface can only be implemented using implements keyword.
8
Access
Abstract class can have any type of members like private, public.
Interface can only have public members.

Example of Abstract Class vs Interface

JavaTester.java

public class JavaTester {
   public static void main(String args[]) {
      Animal tiger = new Tiger();
      tiger.eat();
      Cat lion = new Lion();
      lion.eat();
   }
}
interface Animal {
   public void eat();
}
class Tiger implements Animal {
   public void eat(){
      System.out.println("Tiger eats");
   }
}
abstract class Cat {
   abstract public void eat();
}
class Lion extends Cat{
   public void eat(){
      System.out.println("Lion eats");
   }
}

Output

Tiger eats
Lion eats
raja
Published on 26-Nov-2019 10:07:07
Advertisements