A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/difference-between-abstract-class-and-interface-in-java/ below:

Difference Between Abstract Class and Interface in Java

Difference Between Abstract Class and Interface in Java

Last Updated : 23 Jul, 2025

In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.

In this article, we will learn about abstract class vs interface in Java.

Difference Between Abstract Class and Interface

Points

Abstract Class

Interface

Definition

Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation)

Specifies a set of methods a class must implement; methods are abstract by default.

Implementation Method

Can have both implemented and abstract methods.

Methods are abstract by default; Java 8, can have default and static methods.

Inheritance

class can inherit from only one abstract class.

A class can implement multiple interfaces.

Access Modifiers

Methods and properties can have any access modifier (public, protected, private).

Methods and properties are implicitly public.

Variables

Can have member variables (final, non-final, static, non-static).

Variables are implicitly public, static, and final (constants).

As we know that abstraction refers to hiding the internal implementation of the feature and only showing the functionality to the users. i.e., showing only the required features, and hiding how those features are implemented behind the scene. Whereas, an Interface is another way to achieve abstraction in Java. Both abstract class and interface are used for abstraction. An interface and an Abstract Class are required prerequisites.

Abstract Class in Java

Example: This example demonstrates implementing abstract methods from a common base class (shape) and share common behavior.

Java
// Abstract class Shape, serving as a blueprint for specific shape classes
abstract class Shape {
    // Name of the shape
    String objectName = " "; 

    // Constructor to initialize the name of the shape
    Shape(String name) {
        this.objectName = name;
    }

    // Concrete method to move the shape to a new position
    public void moveTo(int x, int y) {
        System.out.println(this.objectName + " has been moved to x = " + x + " and y = " + y);
    }

    // Abstract method to calculate the area of the shape
    abstract public double area();

    // Abstract method to draw the shape
    abstract public void draw();
}

// Rectangle class extending Shape and providing its own implementation
class Rectangle extends Shape {
     // Dimensions of the rectangle
    int length, width;

    // Constructor to initialize the rectangle's dimensions and name
    Rectangle(int length, int width, String name) {
        super(name);
        this.length = length;
        this.width = width;
    }

    // Implementation of draw method for rectangle
    @Override 
    public void draw() {
        System.out.println("Rectangle has been drawn ");
    }

    // Implementation of area method for rectangle
    @Override 
    public double area() {
        return (double)(length * width);
    }
}

// Circle class extending Shape and providing its own implementation
class Circle extends Shape {
    // Value of pi for circle area calculation
    double pi = 3.14; 
    // Radius of the circle
    int radius; 

    // Constructor to initialize the circle's radius and name
    Circle(int radius, String name) {
        super(name);
        this.radius = radius;
    }

    // Implementation of draw method for circle
    @Override 
    public void draw() {
        System.out.println("Circle has been drawn ");
    }

    // Implementation of area method for circle
    @Override 
    public double area() {
        return (double)(pi * radius * radius);
    }
}

// Main class to test the functionality of the shape classes
public class Geeks {
    public static void main(String[] args) {
        // Creating a Rectangle object and demonstrating its behavior
        Shape rect = new Rectangle(2, 3, "Rectangle");
        System.out.println("Area of rectangle: " + rect.area());
        rect.moveTo(1, 2);

        System.out.println();

        // Creating a Circle object and demonstrating its behavior
        Shape circle = new Circle(2, "Circle");
        System.out.println("Area of circle: " + circle.area());
        circle.moveTo(2, 4);
    }
}

Output
Area of rectangle: 6.0
Rectangle has been moved to x = 1 and y = 2

Area of circle: 12.56
Circle has been moved to x = 2 and y = 4

Example: This example, demonstrating abstract class inheritance and method overriding.

Java
// Abstract class Sunstar
abstract class Sunstar {
    // Abstract method printInfo
    abstract void printInfo();
}

// Employee class that extends the abstract class Sunstar
class Employee extends Sunstar {
    // Implementation of the abstract method printInfo
    void printInfo() {
        // Employee name
        String name = "Avinash"; 
        // Employee age
        int age = 21; 
        // Employee salary
        float salary = 222.2F; 
         // Print name
        System.out.println(name);
        // Print age
        System.out.println(age); 
        // Print salary
        System.out.println(salary); 
    }
}

// Base class to test the implementation
class Base {
    public static void main(String args[]) {
        // Create an Employee object
        Sunstar s = new Employee(); 
        // Call the printInfo method
        s.printInfo(); 
    }
}
Features of an Abstract Class in Java

An abstract class in Java is a special type of class that cannot be instantiated directly and serves as a blueprint for other classes. It provides a way to define a common interface or behavior that can be shared by multiple related classes, but with specific implementations in each derived class.

Key features:

Overall, abstract classes in Java are used to define a common interface or behavior that can be shared by multiple related classes, with specific implementations provided by each derived class.

Interface in Java

Example: This example demonstrates interface implementation.

Java
// Interface defining the method to draw an object
interface Drawable {
    void draw();
}

// Interface defining the method to move an object to a new position
interface Movable {
    void moveTo(int x, int y);
}

// Circle class implementing both Drawable and Movable interfaces
class Circle implements Drawable, Movable {
    double pi = 3.14; // Value of Pi for circle calculations
    int radius; // Radius of the circle

    // Constructor to initialize the radius of the circle
    Circle(int radius) {
        this.radius = radius;
    }

    // Implementation of the draw method from Drawable interface
    @Override 
    public void draw() {
        System.out.println("Circle has been drawn ");
    }

    // Implementation of the moveTo method from Movable interface
    @Override 
    public void moveTo(int x, int y) {
        System.out.println("Circle has been moved to x = " + x + " and y = " + y);
    }
}

// Main class to test the implementation of Circle class
public class Geeks {
    public static void main(String[] args) {
        // Create a Circle object
        Circle circle = new Circle(2);
        
        // Call the draw method
        circle.draw();
        
        // Call the moveTo method
        circle.moveTo(2, 4);
    }
}

Output
Circle has been drawn 
Circle has been moved to x = 2 and y = 4
Features of an Interface in Java

An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields. The methods in interfaces are abstract by default (except for default and static methods introduced in Java 8 and private methods in Java 9).

Key features:

Overall, interfaces in Java are powerful tools for defining contracts and promoting flexibility, interoperability, and maintainability in software design.

Abstract class vs Interface 

Points

Abstract Class

Interface

Type of Methods

Can have both abstract and concrete methods

Can have only abstract methods (until Java 7), and from Java 8, can have default and static methods, and from Java 9, can have private methods.

Variables

Can have final, non-final, static, and non-static variables.

Only static and final variables

Inheritance

Can extend only one class (abstract or not)

A class can implement multiple interfaces

Constructors

Can have constructors

Cannot have constructors

Implementation

Can provide the implementation of interface methods

Cannot provide the implementation of abstract class methods

By understanding these distinctions, you can make informed decisions about when to use abstract classes and interfaces in Java programming.

When to use what?

Consider using abstract classes if any of these statements apply to your situation:  

Consider using interfaces if any of these statements apply to your situation:  



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4