Abstract Classes and Interfaces in Java
In Java, Abstract Classes and Interfaces are powerful tools for creating structured and reusable code. Understanding how to use them effectively helps to create better-designed applications. Let’s dive into the details of both concepts.
Abstract Classes
An abstract class is a class that cannot be instantiated directly. This means you cannot create objects from an abstract class. You use an abstract class as a base class that other classes extend.
Keyword:
extends
Purpose: Provide a base structure that other classes can build upon.
Key Point: An abstract class can have both abstract methods (methods without implementation) and non-abstract methods (methods with implementation).
public abstract class Animal {
int age;
String name;
// Abstract method (must be implemented by subclasses)
public abstract void makeNoise();
// Non-abstract method (optional to override in subclasses)
public void printName() {
System.out.println("Hey Ritik");
}
}
Here, Animal
is an abstract class with:
Abstract Method:
makeNoise()
– must be implemented by any subclass.Non-Abstract Method:
printName()
– can be used as-is or overridden by subclasses.
public class Cat extends Animal {
// Implementing the abstract method from the Animal class
public void makeNoise() {
System.out.println("Meow");
}
}
Now, the Cat
class extends the abstract Animal
class and provides an implementation for the makeNoise()
method.
public class AbstractClass {
public static void main(String[] args) {
Cat myCat = new Cat(); // Creating an object of the subclass Cat
myCat.makeNoise(); // Output: Meow
myCat.printName(); // Output: Hey Ritik
}
}
In the code above:
You can’t instantiate
Animal
directly (because it’s abstract), but you can instantiateCat
and call its methods.The
Cat
class must provide an implementation for themakeNoise()
method since it extends the abstract class.
Key Points About Abstract Classes:
You cannot create objects of an abstract class.
If a class extends an abstract class, it must implement all the abstract methods from the abstract class.
Abstract classes can have both abstract and non-abstract methods.
Abstract classes allow flexibility in how subclasses implement common behaviors.
Interfaces
An interface is a contract that defines a set of methods that implementing classes must provide. All methods in an interface are implicitly abstract, meaning they don’t have an implementation in the interface itself.
Keyword:
implements
Purpose: To define methods that a class must implement.
public interface Animal { int age = 1; // This is static and final String name = "Ritik"; // This is also static and final void makeNoise(); // This is implicitly abstract }
Here,
Animal
is an interface with:Static and Final Fields:
age
andname
– these are constants and must have values.Abstract Method:
makeNoise()
– any class that implements this interface must provide an implementation for this method.
public class Cat implements Animal {
// Implementing the abstract method from the Animal interface
public void makeNoise() {
System.out.println("Meow");
}
public void thisClassOwnMethod() {
System.out.println("Kuch bhi Noise");
}
}
Now, the Cat
class implements the Animal
interface and provides an implementation for the makeNoise()
method.
public class InterfaceExample {
public static void main(String[] args) {
Cat myCat = new Cat(); // Creating an object of the class Cat
myCat.makeNoise(); // Output: Meow
myCat.thisClassOwnMethod(); // Output: Kuch bhi Noise
}
}
In the code above:
You must implement all the methods in the interface when a class implements an interface.
Fields inside an interface are constants (static and final), so they can’t be changed in the implementing classes.
Key Points About Interfaces:
All methods in an interface are implicitly abstract.
Fields in an interface are always static and final.
A class can implement multiple interfaces, allowing for more flexibility.
Differences Between Abstract Classes and Interfaces
Feature | Abstract Class | Interface |
Keyword | extends | implements |
Methods | Can have both abstract and non-abstract methods | All methods are implicitly abstract |
Fields | Can have instance variables | All fields are static and final |
Multiple Inheritance | A class can extend only one abstract class | A class can implement multiple interfaces |
Constructor | Can have constructors | Cannot have constructors |
Key Differences:
Abstract classes allow for more structure and can include both abstract and non-abstract methods. You can also define instance variables.
Interfaces are more rigid; every method is abstract, and all fields are static and final constants.
A class can only extend one abstract class but can implement multiple interfaces.
When to Use Abstract Classes vs Interfaces
Use Abstract Classes when you have a class that should provide a base structure but allow subclasses to share some common code (via non-abstract methods).
Use Interfaces when you need to ensure that a class follows a specific contract (like defining certain methods) but still allows the class to inherit from other classes.
By understanding the distinction between abstract classes and interfaces, you can design better-organized and more flexible Java applications.
Subscribe to my newsletter
Read articles from Ritik Chandel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by