DEV Community

Cover image for Difference Between Abstract Class and Interface in Java
Gowtham Kalyan
Gowtham Kalyan

Posted on

Difference Between Abstract Class and Interface in Java

In Java, both abstract classes and interfaces are used to achieve abstraction. Many beginners get confused between these two concepts because they look similar, but they serve different purposes in application design.

Understanding their differences is very important for Java interviews and real-world development.


What is an Abstract Class?

An abstract class is a class declared using the abstract keyword. It can contain both:

  • Abstract methods (without implementation)
  • Concrete methods (with implementation)

An abstract class is used when classes share a common base with partial behavior already defined.

Example

abstract class Animal {

    abstract void sound();

    void eat() {
        System.out.println("Animal eats food");
    }
}
Enter fullscreen mode Exit fullscreen mode

What is an Interface?

An interface is a blueprint of a class that contains method declarations. The implementing class provides the method implementation.

Interfaces are mainly used to achieve full abstraction and multiple inheritance.

Example

interface Vehicle {
    void start();
}

class Car implements Vehicle {
    public void start() {
        System.out.println("Car starts");
    }
}
Enter fullscreen mode Exit fullscreen mode

Key Differences Between Abstract Class and Interface

Feature Abstract Class Interface
Keyword abstract interface
Methods Abstract + Concrete methods allowed Mostly abstract methods
Variables Can have instance variables Variables are public, static, final by default
Constructors Allowed Not allowed
Access Modifiers Any access modifier Methods are public by default
Multiple Inheritance Not supported (with classes) Supported
Implementation Use extends Use implements
Purpose Partial abstraction Full abstraction / contract

When to Use Abstract Class?

Use an abstract class when:

  • Classes share common code
  • You want default behavior
  • There is a strong parent-child relationship

Example: Animal → Dog, Cat


When to Use Interface?

Use an interface when:

  • Different classes share common capability
  • Multiple inheritance is required
  • You want to define a contract

Example: Flyable → Bird, Airplane


Interview Tip

A simple answer:

An abstract class provides partial abstraction with both implemented and unimplemented methods, while an interface provides a contract that classes must implement and supports multiple inheritance.


Conclusion

Both abstract classes and interfaces are powerful tools in Java design. Choosing between them depends on whether you need shared implementation (abstract class) or a common contract across unrelated classes (interface).


🚀 No 1 Java Real Time Projects Online Training in Hyderabad — ashok it

Want to learn Java concepts with real-time industry projects and practical implementation?

Join our job-oriented Java Full Stack training program guided by real time experts.

✔ Core Java & Advanced Java
✔ Spring Boot & REST APIs
✔ Microservices Architecture
✔ Real-Time Industry Projects
✔ Interview Preparation & Placement Assistance

Start your journey toward becoming a professional Java Full Stack Developer.


=

Top comments (0)