1. From Reality to Code: The Concept of Abstraction¶
Imagine visiting a zoo and seeing various animals: cats, dogs, birds. They all belong to the broader category of “animals,” sharing common characteristics (e.g., they breathe, they are alive), but exhibiting different behaviors (cats “meow,” dogs “woof,” birds “chitter”).
In Java, we can abstract “animals” into a template. This template includes common features of all animals (e.g., “making a sound”) but does not specify how the sound is made (since different animals make sounds differently). This template is an abstract class, and “how the sound is made” is an abstract method (it defines “what to do” but not “how to do it”).
2. Why Define Abstract Classes?¶
The core role of an abstract class is to provide a general template while enforcing that subclasses implement specific methods. Specifically, there are 3 reasons:
- Unified Behavioral Norms: For example, all animals must “make a sound.” An abstract class mandates that the method
makeSound()must exist, ensuring subclasses cannot omit it and maintaining consistent code style. - Avoid Incomplete Objects: An abstract class cannot be directly instantiated (e.g., “animal” itself does not exist; only specific animals like cats or dogs do). It forces subclasses to implement details, preventing creation of incomplete objects.
- Code Reusability: An abstract class can contain common attributes and methods (e.g., an
ageattribute and aneat()method for all animals). Subclasses inherit these directly, avoiding redundant code.
3. Basic Syntax Analysis¶
3.1 Defining an Abstract Class¶
Use the abstract keyword to modify a class. Syntax:
abstract class ClassName {
// Abstract method (declaration only, no implementation)
public abstract void methodName();
// Non-abstract method (can have a concrete implementation)
public void commonMethod() {
System.out.println("This is a common method");
}
}
Notes:
- An abstract class can have regular attributes (e.g., int age;) and non-abstract methods.
- An abstract class cannot be directly instantiated with new (e.g., new ClassName() is invalid). It can only be subclassed.
3.2 Defining an Abstract Method¶
Use the abstract keyword to modify a method. Syntax:
public abstract void methodName(); // Only declaration, no body or logic
Notes:
- An abstract method must be implemented by subclasses (unless the subclass is also abstract).
- An abstract method cannot be private (subclasses cannot access private methods) or static (static methods belong to the class, but abstract methods have no implementation, creating a conflict).
3.3 Subclassing an Abstract Class¶
A subclass must implement all abstract methods of the abstract class; otherwise, the subclass must also be declared abstract. Example:
// Abstract class: Animal
abstract class Animal {
public abstract void makeSound(); // Abstract method: What sound? (unimplemented)
public void eat() { // Non-abstract method: Eating (concrete implementation)
System.out.println("The animal is eating");
}
}
// Subclass: Dog (extends Animal)
class Dog extends Animal {
// Must implement the abstract method makeSound()
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
// Subclass: Cat (extends Animal)
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Meow!");
}
}
4. Example Code: Implementing “Animal Sounds” with Abstract Classes¶
// Abstract class: Animal
abstract class Animal {
// Abstract method: Mandates subclasses implement "how to make a sound"
public abstract void makeSound();
// Non-abstract method: All animals share this behavior
public void eat() {
System.out.println("The animal is eating");
}
}
// Subclass: Dog
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Dog: Woof!");
}
}
// Subclass: Cat
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Cat: Meow!");
}
}
// Test class
public class TestAnimal {
public static void main(String[] args) {
// Only subclasses can be instantiated (abstract class cannot be new)
Animal dog = new Dog();
Animal cat = new Cat();
dog.makeSound(); // Output: Dog: Woof!
cat.makeSound(); // Output: Cat: Meow!
dog.eat(); // Output: The animal is eating (inherited common method)
}
}
5. Key Takeaways¶
- Abstract Class Definition: Declared with
abstract class, cannot be instantiated directly. - Abstract Methods: Declared with
abstract, no method body, and must be implemented by subclasses. - Subclass Rules: A non-abstract subclass must implement all abstract methods; an abstract subclass may inherit unimplemented abstract methods.
- Common Methods: Abstract classes can include regular methods and attributes, which subclasses reuse directly.
Reminder: Java enforces single inheritance for classes (a subclass can extend only one abstract class). However, it can implement multiple interfaces (covered later). If you need to force subclasses to implement specific methods, an abstract class is a more flexible choice than an interface!