An abstract class is a class that cannot be instantiated on its own and may contain abstract methods (methods without a body) and non-abstract methods (methods with a body).
Usage: Used when you want to provide a common base class for other classes to extend, while also providing some shared implementation.
Example
public abstract class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Interface
An interface is a reference type in Java that is used to specify a set of abstract methods that a class must implement. From Java 8 onwards, interfaces can also contain default and static methods with implementations.
Usage: Used to define a contract that other classes must follow. Interfaces are typically used to represent capabilities or behaviors that can be shared across different classes.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Abstract Class
An abstract class is a class that cannot be instantiated on its own and may contain abstract methods (methods without a body) and non-abstract methods (methods with a body).
Example
Interface
An interface is a reference type in Java that is used to specify a set of abstract methods that a class must implement. From Java 8 onwards, interfaces can also contain default and static methods with implementations.
Example
Key Differences
Read more
When to use Interface?
What's the difference between an Abstract Class and an Interface in Java?
What is the purpose of an interface in OOP?