Java Abstract Classes and Methods
What is Abstraction
Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction is an important concept of object-oriented programming that allows us to hide unnecessary details and only show the needed information.
This allows us to manage complexity by omitting or hiding details with a simpler, higher-level idea.
A practical example of abstraction can be motorbike brakes. We know what brake does. When we apply the brake, the motorbike will stop. However, the working of the brake is kept hidden from us.
The major advantage of hiding the working of the brake is that now the manufacturer can implement brake differently for different motorbikes, however, what brake does will be the same.
Let’s take an example that helps us to better understand Java abstraction.
Example 1: Java Abstraction
abstract class MotorBike { abstract void brake(); } class SportsBike extends MotorBike { // implementation of abstract method public void brake() { System.out.println("SportsBike Brake"); } } class MountainBike extends MotorBike { // implementation of abstract method public void brake() { System.out.println("MountainBike Brake"); } } class Main { public static void main(String[] args) { MountainBike m1 = new MountainBike(); m1.brake(); SportsBike s1 = new SportsBike(); s1.brake(); } } /* MountainBike Brake SportsBike Brake */
In the above example, we have created an abstract super class MotorBike. The superclass MotorBike has an abstract method brake().
The brake() method cannot be implemented inside MotorBike. It is because every bike has different implementation of brakes. So, all the subclasses of MotorBike would have different implementation of brake().
So, the implementation of brake() in MotorBike is kept hidden.
Here, MountainBike makes its own implementation of brake() and SportsBike makes its own implementation of brake().
The abstract keyword is a non-access modifier, used for classes and methods:
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class by the extends keyword).
Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
An abstract class can have both abstract and regular methods, the regular method is accessible by the extended class object
abstract class Animal { public abstract void animalSound(); //abstract method public void sleep() { //Regular method System.out.println("Zzz"); } } // Subclass (inherit from Animal) class Pig extends Animal { public void animalSound() { // The body of animalSound() is provided here System.out.println("The pig says: wee wee"); } } class Main { public static void main(String[] args) { Pig myPig = new Pig(); // Create a Pig object myPig.animalSound(); myPig.sleep(); } }
How Abstraction Work
➜ We use the abstract keyword to create abstract classes and methods.
➜An abstract method doesn’t have any implementation (method body).
➜ A class containing abstract methods should also be abstract.
➜ We cannot create objects of an abstract class.
➜ To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass.
➜ A subclass must override all abstract methods of an abstract class. However, if the subclass is declared abstract, it’s not mandatory to override abstract methods.
➜ We can access the static attributes and methods of an abstract class using the reference of the abstract class. For example,
Animal.staticMethod();
Implementing Abstract Methods
If the abstract class includes any abstract method, then all the child classes inherited from the abstract superclass must provide the implementation of the abstract method. For example,
abstract class Animal { abstract void makeSound(); public void eat() { System.out.println("I can eat."); } } class Dog extends Animal { // provide implementation of abstract method public void makeSound() { System.out.println("Bark bark"); } } class Main { public static void main(String[] args) { // create an object of Dog class Dog d1 = new Dog(); d1.makeSound(); d1.eat(); } }
Accesses Constructor of Abstract Classes
An abstract class can have constructors like the regular class. And, we can access the constructor of an abstract class from the subclass using the super keyword. For example,
abstract class Animal { Animal() { …. } } class Dog extends Animal { Dog() { super(); ... } }
Here, we have used the super() inside the constructor of Dog to access the constructor of the Animal.
Note that the super should always be the first statement of the subclass constructor.