Define Data Abstraction in java & Abstract Class: In this Java tutorial, we are going to discuss one of the important OOPS concept called Abstraction. It is a property of OOPS by which we are displaying the essential details to the user and non-essential details are not displayed to the user. Let’s take an example of Bike for this user is able to see the complete bike only but the inner implementation is not displayed to the biker.
We can also say that in this is a process of identifying the required details and ignoring the unnecessary details. let take a real-time example of the bike where the rider only knows how to ride the bike but he does not know when he raised the accelerator or change the gear what’s happening inside of the bike. This is what abstraction is.
Read: More Java Tutorials
In Java, we can achieve the abstraction by using interfaces and abstract classes. If our main motto is to achieve 100% abstraction then we have to use interfaces.
What are the Abstract class and Abstract methods?
- A Call is called an abstract’ class when that class is declared with an abstract keyword.
- An abstract method is a method that does have any method body and that method is declared with the keyword Abstract.
- An Abstract class have all abstract method or it can have also a concrete method (method with method body)
- An abstract method must be implemented in the subclass otherwise you have to make the subclass also an abstract class.
- We cannot create an object for an abstract class and an abstract class cannot be instantiated with the new operator.
- 6 Abstract classes can have a default constructor and parameterized constructor.
When to use Abstract method and abstract class?
Suppose there is a situation where you have the structure than in that place you can create the superclass as the abstract class so we can define methods with the body and some methods without method body. And sometimes we have some general form and that should be shared by all subclasses then, in that case, we can define the superclass as an abstract class and all methods as abstract methods, so that each subclass needs to implement the same abstract methods as per their requirements.
Don’t Miss: Testing Interview Questions
If you think about the interview point of view then in most of the interviews you may face a common question that what is the difference between Encapsulation vs. Data Abstraction.
Encapsulation: It’s a process of hiding data (information hiding), where we groups data and methods together which are acted upon the data.
Data Abstraction: It’s the process of details hiding (implementation hiding) that deals with exposing the interface to the end-user and hiding the inner implementation.
- By using abstraction we can able to reduce the complexity of viewing things.
- We can able to increase the code re-usability and avoid code duplication.
- One of the important things is that we can increase the security of an application and also available the important details to the user.
How to achieve abstraction in java?
As we know that abstraction is one of the core principles concept of Object-oriented programming language and Java following all OOPs principles, abstraction is one of the major building blocks of java language.
So we can achieve abstraction by using interfaces and an abstract class. by using we can achieve 100% abstraction but if we are using an abstract class then we achieve partially abstraction as well.
Please write comments if you find anything incorrect, or you want to share more information about this topic discussed above then you can use our contact us page.