Abstract classes and interfaces are powerful tools in object-oriented programming. They help create flexible, reusable code structures by defining common behaviors and contracts for related classes. These concepts are crucial for understanding inheritance and polymorphism in Java.
In this section, we'll explore how abstract classes provide partial implementations and blueprints for subclasses. We'll also dive into interfaces, which define contracts for classes and enable a form of multiple inheritance in Java.
Abstract Classes
Defining Abstract Classes and Methods
- Abstract class serves as a blueprint for other classes, cannot be instantiated directly
- Contains abstract methods without implementation, forcing subclasses to provide concrete implementations
- May include non-abstract methods with full implementations
- Abstract methods declared using
abstract
keyword, end with semicolon instead of method body - Subclasses must implement all abstract methods or be declared abstract themselves
Creating and Using Concrete Classes
- Concrete class extends abstract class, provides implementations for all abstract methods
- Can be instantiated to create objects
- Inherits non-abstract methods and variables from abstract superclass
- Uses
extends
keyword to inherit from abstract class - Can override inherited methods to provide specialized behavior
Implementation and Polymorphism
- Abstract classes enable polymorphism, allowing objects of different subclasses to be treated uniformly
- Promote code reuse by defining common attributes and behaviors in abstract superclass
- Support partial implementations, useful for defining framework or template for related classes
- Cannot be instantiated using
new
keyword, but can be used as reference type - Abstract class constructors called when concrete subclasses are instantiated
Interfaces
Defining and Implementing Interfaces
- Interface declares a set of abstract methods without implementations
- Serves as a contract for classes that implement it
- Declared using
interface
keyword instead ofclass
- Methods in interfaces implicitly
public
andabstract
- Classes implement interfaces using
implements
keyword - Can contain constant variables (implicitly
public
,static
, andfinal
)
Multiple Interface Inheritance
- Java supports multiple interface inheritance, allowing a class to implement multiple interfaces
- Enables a form of multiple inheritance without the complexity of multiple class inheritance
- Class must provide implementations for all methods from all implemented interfaces
- Resolves diamond problem by requiring explicit implementation of conflicting methods
- Interfaces can extend other interfaces using
extends
keyword
Default and Static Methods in Interfaces
- Default methods introduced in Java 8, provide default implementation in interfaces
- Declared using
default
keyword, have method body unlike traditional interface methods - Allow adding new methods to interfaces without breaking existing implementations
- Static methods in interfaces can be called without an instance of the implementing class
- Both default and static methods can be overridden in implementing classes if needed