BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Difference Between Abstract Class and Interface in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this article, we will discuss the difference between Abstract Class and Interface in Java with examples. I have covered the abstract class and interface in separate tutorials of OOPs Concepts so I would recommend you to read them first, before going though the differences.
1. Abstract class in java
2. Interface in Java

 Abstract Class  Interface
1  An abstract class can extend only one class or one abstract class at a time  An interface can extend any number of interfaces at a time
2   An abstract class can extend another concrete (regular) class or abstract class  An interface can only extend another interface
3  An abstract class can have both abstract and concrete methods  An interface can have only abstract methods
4  In abstract class keyword “abstract” is mandatory to declare a method as an abstract  In an interface keyword “abstract” is optional to declare a method as an abstract
5  An abstract class can have protected and public abstract methods  An interface can have only have public abstract methods
6  An abstract class can have static, final or static final variable with any access specifier  interface can only have public static final (constant) variable

Each of the above mentioned points are explained with an example below:

Abstract class vs interface in Java

Difference No.1: Abstract class can extend only one class or one abstract class at a time

class Example1{
   public void display1(){
      System.out.println("display1 method");
   }
}
abstract class Example2{
   public void display2(){
      System.out.println("display2 method");
   }
}
abstract class Example3 extends Example1{
   abstract void display3();
}
class Example4 extends Example3{
   public void display3(){
      System.out.println("display3 method");
   }
}
class Demo{
   public static void main(String args[]){
       Example4 obj=new Example4();
       obj.display3();
   }
}

Output:

display3 method

Interface can extend any number of interfaces at a time

//first interface
interface Example1{
    public void display1();
}
//second interface
interface Example2 {
    public void display2();
}
//This interface is extending both the above interfaces
interface Example3 extends Example1,Example2{
}
class Example4 implements Example3{
    public void display1(){
        System.out.println("display2 method");
    }
    public void display2(){
        System.out.println("display3 method");
    }
}
class Demo{
    public static void main(String args[]){
        Example4 obj=new Example4();
        obj.display1();
    }
}

Output:

display2 method

Difference No.2: Abstract class can be extended(inherited) by a class or an abstract class

class Example1{
   public void display1(){
      System.out.println("display1 method");
   }
}
abstract class Example2{
   public void display2(){
       System.out.println("display2 method");
   }
}
abstract class Example3 extends Example2{
   abstract void display3();
}
class Example4 extends Example3{
   public void display2(){
       System.out.println("Example4-display2 method");
   }
   public void display3(){
       System.out.println("display3 method");
   }
}
class Demo{
   public static void main(String args[]){
       Example4 obj=new Example4();
       obj.display2();
   }
}

Output:

Example4-display2 method

Interfaces can be extended only by interfaces. Classes has to implement them instead of extend

interface Example1{
    public void display1();
}
interface Example2 extends Example1{
}
class Example3 implements Example2{
   public void display1(){
      System.out.println("display1 method");
   }
}
class Demo{
   public static void main(String args[]){
      Example3 obj=new Example3();
      obj.display1();
   }
}

Output:

display1 method

Difference No.3: Abstract class can have both abstract and concrete methods

abstract class Example1 {
   abstract void display1();
   public void display2(){
     System.out.println("display2 method");
   }
}
class Example2 extends Example1{
   public void display1(){
      System.out.println("display1 method");
   }
}
class Demo{
   public static void main(String args[]){
     Example2 obj=new Example2();
     obj.display1();
   }
}

Interface can only have abstract methods, they cannot have concrete methods

interface Example1{
   public abstract void display1();
}
class Example2 implements Example1{
   public void display1(){
      System.out.println("display1 method");
   }
}
class Demo{
   public static void main(String args[]){
      Example2 obj=new Example2();
      obj.display1();
   }
}

Output:

display1 method

Difference No.4: In abstract class, the keyword ‘abstract’ is mandatory to declare a method as an abstract

abstract class Example1{
   public abstract void display1();
}

class Example2 extends Example1{
   public void display1(){
      System.out.println("display1 method");
   }
   public void display2(){
      System.out.println("display2 method");
   }
}
class Demo{
   public static void main(String args[]){ 
       Example2 obj=new Example2(); 
       obj.display1();
   }
}

In interfaces, the keyword ‘abstract’ is optional to declare a method as an abstract because all the methods are abstract by default

interface Example1{
    public void display1();
}
class Example2 implements Example1{
    public void display1(){
        System.out.println("display1 method");
    }
    public void display2(){
        System.out.println("display2 method");
    } 
}
class Demo{
   public static void main(String args[]){
       Example2 obj=new Example2();
       obj.display1();
   }
}

Difference No.5: Abstract class can have protected and public abstract methods

abstract class Example1{
   protected abstract void display1();
   public abstract void display2();
   public abstract void display3();
}
class Example2 extends Example1{
   public void display1(){
       System.out.println("display1 method");
   }
   public void display2(){
      System.out.println("display2 method");
   }
   public void display3(){
      System.out.println("display3 method");
   }
}
class Demo{
   public static void main(String args[]){
      Example2 obj=new Example2();
      obj.display1();
   }
}

Interface can have only public abstract methods

interface Example1{
   void display1();
}
class Example2 implements Example1{
   public void display1(){
      System.out.println("display1 method");
   }
   public void display2(){ 
      System.out.println("display2 method");
   }
}
class Demo{
   public static void main(String args[]){
       Example2 obj=new Example2();
       obj.display1();
   }
}

Difference No.6: Abstract class can have static, final or static final variables with any access specifier

abstract class Example1{
   private int numOne=10;
   protected final int numTwo=20;
   public static final int numThree=500;
   public void display1(){
      System.out.println("Num1="+numOne);
   }
}
class Example2 extends Example1{
   public void display2(){
      System.out.println("Num2="+numTwo);
      System.out.println("Num2="+numThree);
   }
}
class Demo{
   public static void main(String args[]){
      Example2 obj=new Example2(); 
      obj.display1();
      obj.display2();
   }
}

Interface can have only public static final (constant) variable

interface Example1{
   int numOne=10;
}
class Example2 implements Example1{
   public void display1(){
      System.out.println("Num1="+numOne);
   }
}
class Demo{
   public static void main(String args[]){
      Example2 obj=new Example2();
      obj.display1();
   }
}
❮ PreviousNext ❯

Top Related Articles:

  1. Inner classes in java: Anonymous inner and static nested class
  2. Abstract Class in Java with example
  3. Java Functional Interfaces
  4. Constructor Overloading in Java with examples
  5. Java – Static Class, Block, Methods and Variables

Tags: Java-OOPs

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Pari says

    November 4, 2014 at 6:31 PM

    I love your site. all the java tutorial is excellent and easy to understand , Good work Keep going

    Reply
  2. saravana says

    November 26, 2014 at 5:21 AM

    Thank so much,i like all the tutorials keep rocking……..

    Reply
  3. kanchana says

    February 25, 2015 at 2:35 PM

    This website is really nice..i can easily understand the basic concept of java. thank u…..

    Reply
  4. Utkal says

    March 25, 2015 at 6:50 AM

    Could you please provide one synario where we can go for abstract class
    and we ll not go for it Interface

    Reply
  5. Ivkaran Singh says

    March 27, 2015 at 2:53 AM

    Your website has done an excellent job explaining the Java features. Really easy to understand and follow, helping me a lot while preparing for interviews.

    Reply
  6. jocie says

    March 30, 2015 at 4:57 PM

    Really superb explanation… Very good material to use in class room

    Reply
  7. satyajit says

    May 7, 2015 at 7:58 PM

    please explain abstraction with a simple example

    Reply
  8. BHUSHAN says

    June 22, 2015 at 6:27 PM

    Awsome site!!

    Reply
  9. Rupesh says

    July 20, 2015 at 5:37 AM

    Easy to understand. nice example short and sweet. Keep going..

    Reply
  10. Divyanshu Ranjan says

    August 27, 2015 at 9:07 PM

    Love your site.. Too much easy and understandable language.. With short and very easy example..

    Reply
  11. Manish says

    November 13, 2015 at 1:35 PM

    Hello All,
    Interface can have static method as well as method with default keyword

    Reply
  12. deepali says

    November 19, 2015 at 11:41 AM

    Can abstract class implements interface?

    Reply
  13. Tonoy says

    December 4, 2015 at 12:50 AM

    Awesome site for learning, very much organized and nicely presented. Keep it up. cheers!

    Reply
  14. Nadia khan says

    December 10, 2015 at 4:01 AM

    superb material and easy to understand …

    Reply
  15. chandramohan says

    March 7, 2016 at 10:14 AM

    i have a one doubt in long time….that was why we are using a interface in java…what is the main reason…all the interface example programs are i will tried at same time i remove the interface in that program when the program will be run so fully confused…tell me please clearly..what is the main reason for using interface in java

    Reply
  16. Xiu Chen says

    March 17, 2016 at 9:13 PM

    Really like to read your website. Clear and easy~ thank your very much!!! ^_^

    Reply
  17. Kapil says

    March 30, 2016 at 1:07 PM

    Can u pls explain with example when to use Abstract Class and Interface.

    Reply
  18. Trilok SIngh says

    May 25, 2016 at 3:41 AM

    What is the main advantage of abstract class.. why we use this …

    Reply
  19. Zia ur Rehman says

    June 16, 2016 at 11:13 PM

    Awesome. Excellent … Nice and precious Tutorial.
    Good job! keep it up.

    Reply
  20. Luvnish Monga says

    November 30, 2016 at 12:05 PM

    This tutorial is very good. Great Job (Y) It clears all the concepts of Abstract Classes as well of Interface.

    Reply
  21. Geetha says

    January 21, 2017 at 9:49 AM

    superb site, easy to understand…..thank u….keep rocking!! :)

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap