Abstraction

Abstraction is the concept of hiding the internal details of a functionality and providing a simple representation for the same.

So complex functionality can be made available to the outside world in a simple way.

We can see below that internal details of a car is abstracted and its functionality is made available to public in a much simpler way.

Abstraction

Few Examples:


We use mobile phone everyday but we don’t know how the functionalities are designed inside it so that we receive the calls and send messages etc.

These functionalities have been kept inside and we are just accessing them using the options provided in the mobile.

Similarly, we drive a car and change the gears as and when its required to go high speed and come back to low speed but We don’t know how the gear box works internally.

So, gearbox working is abstracted and provided with simple mechanism of changing gears to the outside world.

Similarly, whenever we want to stop the car then we apply brakes but we are not sure how the braking system works internally.

Similarly,

In java we can write a method to perform some functionality inside a class and we can expose it to outside world just by 

providing an option to call this method.


Anyone who calls this method will not be knowing the internal complexity of the method but will be knowing the functionality of the method and hence he calls it and uses it.

In this way, we hide the internal implementation and abstract it inside a method.

We can achieve abstraction in Java using 2 ways

1) Abstract class

2) Interface


1) Abstract class


Abstract class in Java can be created using “abstract” keyword.

If we make any class as abstract then it can not be instantiated which means we are not able to create the object of abstract class.

Inside Abstract class, we can declare abstract methods as well as concrete methods

So using abstract class, we can achieve 0 to 100 % abstraction.

Example:

Phone.java

  1. Abstract class Phone{
  2. Abstract void receiveCall();
  3. Abstract void sendMessage();
  4. }
Abstract class Phone{
Abstract void receiveCall();
Abstract void sendMessage();
}


Now any concrete class which extends the above abstract class will provide the definition of these abstract methods.

This definition has the actual internal details of how this functionality has been implemented.

Anyone who needs to access this functionality has to call the method using the Phone object pointing to its subclass.

Check Abstract class in Java article for more details on Abstract class.



2) Interface


Interface is used to achieve pure or complete abstraction.

We will have all the methods declared inside Interface as abstract only.

So, we call interface as 100% abstraction.

Example:


We can define interface for Car functionality abstraction as below

Car.java

  1. Interface Car{
  2. public void changeGear( int gearNumber);
  3. public void applyBrakes();
  4. }
Interface Car{
public void changeGear( int gearNumber);
public void applyBrakes();
}


Now these functionalities like changing a gear and applying brake are abstracted using this interface.

Actual implementation will be provided by the subclasses which implements the above the interface.

Lamborghini.java

  1. Class Lamborghini implements car {
  2. public void changeGear( int gearNumber){
  3. //Actual logic to change the gear
  4. System.out.println("Gear changed in Lamborghini car to "+gearNumber);
  5.  
  6. }
  7.  
  8. public void applyBrakes(){
  9. //Actual logic to apply the brake
  10. System.out.println("Break applied in Lamborghini car ");
  11.  
  12. }
  13. }
Class Lamborghini implements car {
public void changeGear( int gearNumber){
//Actual logic to change the gear
System.out.println("Gear changed in Lamborghini car to "+gearNumber);

}

public void applyBrakes(){
//Actual logic to apply the brake
System.out.println("Break applied in Lamborghini car ");

}
}


Similarly , we can implement this interface for Audi car

Audi.java

  1. Class Audi implements car {
  2. public void changeGear( int gearNumber){
  3. //Actual logic to change the gear
  4. System.out.println("Gear changed in Audi car to "+gearNumber);
  5.  
  6. }
  7.  
  8. public void applyBrakes(){
  9. //Actual logic to apply the brake
  10. System.out.println("Break applied in Audi car ");
  11.  
  12. }
  13. }
Class Audi implements car {
public void changeGear( int gearNumber){
//Actual logic to change the gear
System.out.println("Gear changed in Audi car to "+gearNumber);

}

public void applyBrakes(){
//Actual logic to apply the brake
System.out.println("Break applied in Audi car ");

}
}


We can see that, internal working of gear box and the brake system is implemented in the subclass and its abstracted using the interface.

Anyone who would like to use this functionality can call appropriate method using interface variable pointing to the required implementation as below

  1. Car c = new Audi();
  2. c. changeGear(2);
  3. c. applyBrakes();
Car c = new Audi();
c. changeGear(2);
c. applyBrakes();


Since variable “c” is pointing to Audi class object , we can see below output


Check interfaces in Java article for more details on interface.


About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on