Java is an object-oriented programming language and it has four major pillars that are an abstraction, encapsulation, inheritance, and polymorphism. In this article, we will discuss abstraction in java and abstraction in java with example.
Here is the table content of the article will we will cover this topic.
1. What is an abstraction in Java?
2. Ways to achieve Abstraction in Java?
3. Abstract class in java?
4. When to use the abstract class?
5. Why can’t we create the object of an abstract class?
6. How to use abstract class and abstract methods in Real life?
7. Abstract method in java?
8. Important rules for abstract methods?
9. Abstract class Vs concrete class?
10. Can abstract class be final in java?
11. Can an abstract class have a constructor?
12 Interface in java?
13. Interface vs Abstract class?
14. When and how to use interface?
What is abstraction?
In simple terms, the mean of Abstraction is to show the necessary things to the user and hide the details. In abstraction, the user will able to access only those parts of information that are necessary for them. This is also known as data abstraction in java.
Real-life example: Suppose a person wants to drive a car. The person only needs to know about some basic things that are necessary for him. The necessary things to drive a car accelerator, clutch, brakes. He doesn’t need to know about the inner mechanism of the car. This is the best example of abstraction.
What is an abstraction in Java?
In java, the abstraction is a process of hiding the implementation details from the user and only provides the functionality to the user. The user will focus on the functionality rather than how it works.
An object contains a large amount of information. By use of Abstraction, we can show only the relevant information of the object to the user. It helps to reduce programming complexity and effort. It is one of the most important concepts of OOPs.
Let’s discuss the abstraction oop and abstraction in oops with example. Let’s see the example of a banking application. In banking application, A customer can see his/her account balance, mini statement, change password option, etc. But we hide the fetch and update operation. Because this will not create any value to the user. He/She doesn’t care about it they just need to use the application functionality.
In java, abstraction is achieved by abstract class and interface. The abstract class provides the 1 to 100% of abstraction but by use of the interface, we can achieve 100% abstraction. We will discuss it later in detail.
Let’s discuss the example of abstraction. Here we will create a Bank class (Parent class) and two child classes called Customer and Employee. Both Customer and Employee can have some common properties like name, age, Name of Bank but there are some properties are different. An Employee and Person can access a different levels of information in the Bank. A customer can get the information only about his account, but an Employee can get information about a number of customers.
Ways to achieve Abstraction in Java
1. Abstract class: A class that is declared with abstract keyword is known as abstract class. An abstract class can have abstract methods (Method without body) and concrete/non-abstact methods (Methods with the body) also. A normal class can’t have any abstract method. In this article, we will discuss the abstract class in java, what are the properties of the abstract class, why we use it?
The abstract class provides the abstraction level from 1 to 100%. The level of abstraction is totally depending upon the abstract methods. If an abstract class has an abstract method and non- abstract method, then the abstraction level lies between 1 to 100. But if an abstract class contains only abstract method then it provides 100% abstraction. We can use any number of abstract methods in the abstract class as per the use. For more detail.
2. Interface: The interface is a way to achieve abstraction in JAVA. An interface is used for full abstraction. An interface is a blueprint of a class. An interface can have methods and variables like class, but the methods of an interface are by default abstract. It means the interface can contain only an abstract method(Method without body). For more detail.
Abstraction is achieved by an interface and an abstract class. We can achieve 100% abstraction using interfaces.