Java Abstraction
In this tutorial, we will learn about abstraction and how abstraction of the OOPs can be achieved in the java with working examples and use case of it.
Java Abstraction
Meaning of abstract is an idea of something which is not physically exists, similarly abstraction in the oops is declaring something for a similar outcome keeping mind or opened for the customization or expecting a change.
But the steps to achieve the outcome will be left blank, outcome can be achieved either with single step or multiple steps.
The main advantage of abstraction is re-usability and scope for customization, consider one example as a car and brand as branded car.
Depending on the customizations or features of the car the price may increase or decrease but driving functionality will not change.
Abstraction in Java with examples:
Java abstraction can be achieved by using a keyword abstract, following is an example of abstraction.
Example:
abstract class Car {
public void drivingFunctionality() {
System.out.println("Working");
}
public void vehicleType() {
System.out.println("Petrol Vehicle");
}
public abstract void addonFeature();
}
public class BrandCar extends Car{
@Override
public void addonFeature() {
System.out.println("Air Condition,seat belt,music system and air bangs enabled");
}
}
The extending class BrandCar need not to know the other features of the Car like drivingFunctionality and VehicleType. Which in turns reduces the complexity for the BrandCar and can concentrate only on addonFeature method.
Note: all interface methods by default all are abstract methods only.When we declare class type as abstract class, this abstract class cannot be initialized and can be initialized by its sub-class otherwise it will fail in the compilation.
Example2:
abstract class Car {
public void drivingFunctionality() {
System.out.println("Working");
}
public void vehicleType() {
System.out.println("Petrol Vehicle");
}
public abstract void addonFeature();
}
public class MainClass {
public static void main(String args[]) {
Car car = new Car();
}
}
The above example will fail with error saying that cannot initiate type Car. In order to avoid the compilation error, Car should be instantiated by different class and this should be child class of the Car.
Below example will be the child class of Car and implements Car abstract methods.
abstract class Car {
public void drivingFunctionality() {
System.out.println("Working");
}
public void vehicleType() {
System.out.println("Petrol Vehicle");
}
public abstract void addonFeature();
}
public class MainClass extends Car{
public static void main(String args[]) {
Car car = new MainClass();
}
@Override
public void addonFeature() {
// We can write the add on features of Car as desired
}
}
Following points to remember while declaring class as abstract in Java.
- Class must be type of abstract.
- One or more methods can be declared as abstract methods.
- Abstract class should not declare as a final class
- Abstract class must be an access type of public or default
- Common feature methods should be written in abstract class
- Custom feature methods should be of an abstract type.
- Implement DRY(Do not repeat yourself) principle.
Conclusion :
In this tutorial, we learned about abstraction, how abstraction can be implemented in Java with working example and points to consider before writing abstract class.