Difference Between Abstract Class and Interface in Java

1. Abstract Class Vs Interface – Objective

In our last tutorial, we studied Pair Class in java. Today, in this tutorial, we are going to learn about the Difference between Abstract Class and Interface in Java. Moreover, we will also cover Java Abstract Class vs Interface with real-time examples.

So, let’s start the Difference Between Abstract Class and Interface in Java.

Difference between Abstract Class and Interface in Java

Difference between Abstract Class and Interface in Java

2. What is Abstract Class Vs Interface in Java

Java Abstract class and Interface is used for abstraction in Java, they are meant to hide the internal implementation and only showing the functionality to the users, i.e. showing what is works and hiding how it works.

3. Difference Between Abstract Class and Interface in Java

Following factors shows the Difference between Abstract Class vs Interface in Java.

Difference between Abstract Class and Interface in Java

Difference between Abstract Class and Interface in Java

a. Types of Java Methods

Abstract class in Java can have both abstract and non-abstract methods but an interface can only have abstract methods.
From the version 8 of Java, the interface supports static and non-static methods too.

b. Final Java Variables

Abstract class in Java may or may not have variables declared as final but in Java interfaces, it is by default that they will declare final.

c. Type of Variable

Java interface can only have static and final variables while abstract class in Java can have both final and non-final as well as static and non-static variables.

Learn more about Variables in Java

d. Implementation

Implementation for Java abstract class cannot be provided by interface but an abstract class can provide an implementation for the interface in Java.

e. Inheritance vs. Abstraction

An abstract class can be extended using the keyword ‘extends’ while a Java interface uses the keyword ‘implements’ to be implemented.

Learn more about Inheritance in Java

f. Multiple Implementations

Java abstract class can implement multiple interfaces and also extend Java class but an interface can only extend another interface in Java.

g. Accessibility of Data Members

Members of an abstract class in Java can be set while members of a Java interface is by default public.

Difference between Abstract Class and Interface in Java

Difference between Abstract Class and Interface in Java

4. Abstract Class and Interface in Java with Examples

After discussing the Difference between Abstract class and Interface in Java, let’s discuss some examples:

a. Abstract class in Java

Import java.io.*;
// abstract class
abstract class Shape
  {
// declare fields
       String objectName = " ";  
       Shape(String name)
          {
             this.objectName = name;
          }   
public void moveTo(int x, int y)
      {
         System.out.println(this.objectName + " " + "has been moved to"
                                   + " x = " + x + " and y = " + y);
      }   
// abstract methods which will be implemented by its subclass(es)
          abstract public double area();
          abstract public void draw();
  }
class Rectangle extends Shape
   {  
       int length, width;  
// constructor
       Rectangle(int length, int width, String name)
           {      
               super(name);
               this.length = length;
               this.width = width;
           }    
@Override
public void draw()
        {
           System.out.println("Rectangle has been drawn ");
        }   
@Override
public double area()
        {
            return (double)(length*width);
        }
    }
class Circle extends Shape
   {  
        double pi = 3.14;
        int radius;   
//constructor
        Circle(int radius, String name)
           {      
              super(name);
              this.radius = radius;
           }   
@Override
public void draw()
          {      
               System.out.println("Circle has been drawn ");
          }
@Override
public double area()
         {
            return (double)((pi*radius*radius)/2);
         }
    }
class GFG
     {
         public static void main (String[] args)
             {
// creating the Object of Rectangle class and using shape class reference.
                   Shape rect = new Rectangle(2,3, "Rectangle");
                   System.out.println("Area of rectangle: " + rect.area());
                   rect.moveTo(1,2);     
                   System.out.println(" ");       
// creating the Objects of circle class
                   Shape circle = new Circle(2, "Cicle");
                   System.out.println("Area of circle: " + circle.area());
                   circle.moveTo(2,4);   
             }
     }

Let’s Discuss Serialization and Deserialization in Java

b. Interface in Java

import java.io.*;
interface Shape
  {
// abstract method
      void draw();
      double area();
  }
class Rectangle implements Shape
   {
      int length, width;
// constructor
      Rectangle(int length, int width)
           {
               this.length = length;
               this.width = width;
           }   
@Override
      public void draw()
           {
              System.out.println("Rectangle has been drawn ");
           }    
@Override
      public double area()
           {
              return (double)(length*width);
           }
    }
class Circle implements Shape
    {  
       double pi = 3.14;
       int radius;
//constructor
       Circle(int radius)
           {     
              this.radius = radius;
           }  
@Override
       public void draw()
           {
               System.out.println("Circle has been drawn ");
           }
@Override
       public double area()
         {       
               return (double)((pi*radius*radius)/2);
         }  
   }
class GFG
   {
        public static void main (String[] args)
            {  
                Shape rect = new Rectangle(2,3);
                System.out.println("Area of rectangle: " + rect.area());
// creating the Objects of circle class
                Shape circle = new Circle(2);
                System.out.println("Area of circle: " + circle.area());
            }
    }

5. When to use what

a. Abstract Class in Java

  • When some classes need to share some codes then we put these classes in abstract classes.
  • When a state of an object is to define because we need to define a non-static or non-final fields.

Read more about Java Inner Class & Java File Class

b. Interface in Java

  • When there is a total abstraction.
  • When multiple inheritances are there.

So, this was all about “Difference between Abstract Class and Interface in Java”. Hope you like our explanation.

6. Conclusion

In this tutorial, we learned the difference between Abstract class and Interface in Java, where to use them and also Abstract class vs Interface in Java real time examples.

See Also – Java Access Modifiers

For reference

Leave a Reply

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