Unlocking the Power of Abstract Classes in Java
What are Abstract Classes?
In Java, abstract classes are a fundamental concept in object-oriented programming. They allow us to create a blueprint for other classes to follow, without being able to instantiate them on their own. To declare an abstract class, we use the abstract
keyword.
public abstract class Language {
// abstract class body
}
Abstract Methods: The Key to Flexibility
An abstract method is a method without a body, declared using the same abstract
keyword. If a class contains an abstract method, it must be declared as abstract. This ensures that any subclass inheriting from it must provide an implementation for the abstract method.
public abstract class Animal {
public abstract void makeSound();
}
Inheriting from Abstract Classes
Although we can’t create objects from abstract classes, we can create subclasses that inherit from them. These subclasses can then access members of the abstract class using their own objects. For example, consider a Language
abstract class with a regular display()
method:
public abstract class Language {
public void display() {
System.out.println("Displaying language...");
}
}
public class Main extends Language {
public static void main(String[] args) {
Main main = new Main();
main.display(); // outputs "Displaying language..."
}
}
Implementing Abstract Methods
When an abstract class includes an abstract method, all child classes must provide an implementation for it. For instance, if we have an Animal
abstract class with an abstract makeSound()
method, any subclass like Dog
must implement this method:
public abstract class Animal {
public abstract void makeSound();
public void eat() {
System.out.println("Eating...");
}
}
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Barking...");
}
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // outputs "Barking..."
dog.eat(); // outputs "Eating..."
}
}
Constructors in Abstract Classes
Abstract classes can have constructors, just like regular classes. We can access these constructors from subclasses using the super
keyword. However, the super
statement must always be the first line in the subclass constructor:
public abstract class Animal {
public Animal(String name) {
System.out.println("Animal constructor: " + name);
}
}
public class Dog extends Animal {
public Dog(String name) {
super(name); // calls Animal constructor
System.out.println("Dog constructor: " + name);
}
public static void main(String[] args) {
Dog dog = new Dog("Fido");
}
}
The Power of Abstraction
Abstract classes and methods enable us to achieve abstraction in Java, a crucial concept in object-oriented programming. Abstraction allows us to hide unnecessary details and show only the required information, making it easier to manage complexity. A practical example is motorbike brakes, where we know what they do but not how they work. This allows manufacturers to implement brakes differently for various motorbikes.
Practical Example: Java Abstraction
Consider an abstract MotorBike
class with an abstract brake()
method. Since every bike has a unique brake implementation, we keep it hidden in the superclass. Subclasses like MountainBike
and SportsBike
can then provide their own implementations of the brake()
method:
public abstract class MotorBike {
public abstract void brake();
}
public class MountainBike extends MotorBike {
@Override
public void brake() {
System.out.println("Mountain bike braking...");
}
}
public class SportsBike extends MotorBike {
@Override
public void brake() {
System.out.println("Sports bike braking...");
}
}
Key Takeaways
- Use the
abstract
keyword to create abstract classes and methods. - An abstract method lacks implementation (method body).
- A class with abstract methods must be declared abstract.
- We can’t create objects from abstract classes.
- To implement features of an abstract class, inherit subclasses and create objects of the subclass.
- A subclass must override all abstract methods of an abstract class, unless it’s also declared abstract.
- We can access static attributes and methods of an abstract class using the reference of the abstract class.