Abstraction in Java

Reading Time: 2 minutes

What is Abstraction?

Abstraction is one of the most important feature of OOPs (Object Oriented Programming). There are four essential features in OOPs i.e., encapsulation, inheritance, abstraction and polymorphism. Here we will discuss about one of the four features i.e., Abstraction.

It is the process of showing necessary details to the user and hiding all the details of implementation. In other words, we can say that it shows only important features to the user and hides the internal functionality.

Examples Of Abstraction

  1. A man driving a car. The driver of the car only knows that pressing the accelerator will increase the speed and applying the brakes will stop the car but driver of the car doesn’t know about the internal working of the accelerator, brakes, and all the internal functioning.
  2. Sending an e-mail. The sender doesn’t know that which protocol is going to be used by the e-mail server to send an e-mail. To send an e-mail sender needs to write the content, add the e-mail address of the receiver and click on the send button.

In Java through interfaces and abstract classes we can achieve abstraction. With the help of interface we are able to achieve 100% abstraction.

Abstract class in Java

To declare a class as an abstract class we use abstract keyword. An Abstract class is a class that can have both abstract and non-abstract methods. It cannot be instantiated.

Syntax:

Example:

Output:

When to use abstract classes and abstract methods

There is a situation in which we define a superclass that declares the structure of abstraction and hides the internal working. Sometimes we have to create a superclass that only defines generalization form, shared by all of its subclasses, leaving it to each subclass to fill in the details.

Advantages

1. It reduces the complexity of viewing the things.

2. It increases the re-usability of code. There is no need to write the same code again and again.

3. It avoids the duplication of code.

4. It helps to increase the security of program as only important details are provided to the user.

Conclusion

Abstraction is the most important feature of Object-Oriented Programming. It is used to show only essential feature to the user.

knoldus

Written by 

My name is Shivam Mehta. I am working as a Software Intern in Java Studio at Knoldus. My hobbies are playing volleyball and football.

1 thought on “Abstraction in Java3 min read

Comments are closed.