Abstract Classes and Interfaces
Learn about abstract classes, interfaces, and their role in achieving abstraction and defining contracts.
Understanding Java Interfaces
Introduction to Interfaces
In Java, an interface is a blueprint of a class. It specifies what a class must do but not how. It's a collection of abstract methods (methods without a body) and constant variables. Essentially, it defines a contract that classes can implement.
Defining Interfaces
An interface is defined using the interface
keyword. All methods in an interface are implicitly public
and abstract
. Variables declared in an interface are implicitly public
, static
, and final
.
public interface MyInterface {
// Constant variable
int MY_CONSTANT = 10;
// Abstract method (no body)
void doSomething();
// Another abstract method
String getDescription();
}
Notice that the methods doSomething()
and getDescription()
do not have an implementation (no curly braces {}
containing code). They are only declarations.
Implementing Interfaces
A class implements an interface to signify that it will provide the implementation for all the abstract methods defined in that interface. A class can implement multiple interfaces.
public class MyClass implements MyInterface {
@Override
public void doSomething() {
System.out.println("Doing something...");
}
@Override
public String getDescription() {
return "This is MyClass.";
}
}
The @Override
annotation is optional but highly recommended. It helps the compiler verify that you are correctly implementing a method from the interface. If you forget to implement a method from the interface, or if the method signature doesn't match, the compiler will throw an error.
The Role of Interfaces: Defining Contracts
The primary role of interfaces is to define a contract. The interface specifies what functionality a class *must* provide. Any class that implements the interface guarantees that it will provide an implementation for all the methods defined in the interface.
This promotes loose coupling and polymorphism. You can write code that operates on objects of different classes, as long as those classes implement the same interface. Your code doesn't need to know the specific class of the object; it only needs to know that the object provides the functionality defined in the interface.
public interface Animal {
void makeSound();
}
public class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
public class Cat implements Animal {
@Override
public void makeSound() {
System.out.println("Meow!");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.makeSound(); // Output: Woof!
myCat.makeSound(); // Output: Meow!
}
}
In the example above, the Main
class doesn't need to know if it's dealing with a Dog
or a Cat
. It only knows that both classes implement the Animal
interface and therefore have a makeSound()
method.
Interfaces vs. Abstract Classes
Both interfaces and abstract classes can define abstract methods, but there are key differences:
- Multiple Inheritance: A class can implement multiple interfaces, but it can only inherit from one abstract class (or class in general).
- Implementation: Interfaces cannot have any method implementations (except for default methods and static methods introduced in Java 8). Abstract classes can have both abstract and non-abstract (implemented) methods.
- Variables: Variables in an interface are implicitly
public static final
(constants). Abstract classes can have variables with any access modifier (private
,protected
,public
) and can be mutable (notfinal
). - Purpose: Interfaces are primarily used to define a contract or a set of functionalities that a class *must* implement. Abstract classes are used to provide a base class with some common implementation that subclasses can inherit and extend.
Feature | Interface | Abstract Class |
---|---|---|
Multiple Inheritance | Supported | Not Supported |
Method Implementation | No (except default & static methods) | Yes (can have both abstract and non-abstract methods) |
Variables | public static final (constants) | Can have any access modifier and can be mutable |
Primary Purpose | Define a contract | Provide a base class with common implementation |
When to use which?
- Use an interface when you want to define a role or capability that multiple unrelated classes can fulfill.
- Use an abstract class when you want to provide a common base class with some shared implementation for a group of related classes.