Abstraction
What is Abstraction?
In simple terms, abstraction “displays” only the relevant attributes of objects and “hides” the unnecessary details.
Data abstraction in Java helps the developers hide the code complications from the end-user by reducing the project’s complete characteristics to only the necessary components.
Abstraction in OOP can be of two types:
- Data Abstraction
- Control Abstraction
Data Abstraction:
In data abstraction, we mostly create complex data types and hide their implementation. We only expose the operations to manipulate these data types without going into the details of their implementation.
One advantage of this approach is that we can change the implementation anytime without changing the behavior that is exposed to the user.
Control Abstraction:
Control abstraction collects all the control statements that are a part of the application and exposes them as a unit. This feature is used when we have to perform a working feature using this control unit.
Control abstraction forms the main unit of structured programming and using control abstraction we can define simple functions in complex frameworks.
What Is Abstract Class in Java?
Java implements abstraction using abstract classes and interfaces. Let’s first explore all about the abstract class.
An abstract class can be defined as a class declared with the keyword “abstract” and has a restriction that it cannot be instantiated.
An abstract class may or may not have any abstract method (a method with no implementation). As far as JVM is concerned, an abstract class is an incomplete class that does not have a complete behavior.
The general syntax of an abstract class is given below:
abstract class <classname>
{
public abstract void abstractMethod();
public void normalMethod()
{
//method body
}
}
As shown in the syntax of the above abstract class, we can have abstract as well as non-abstract methods in an abstract class. The keyword ‘abstract’ precedes the class declaration.
What is Abstract Method in Java?
An abstract method is a method preceded by an ‘abstract’ keyword without any implementation. An abstract method is declared inside an abstract class.
An abstract method is the one that makes a class incomplete as it doesn’t have an implementation. Hence when we include an abstract method in the class, naturally the class becomes incomplete.
We can use the abstract method by implementing it in a subclass i.e. a class inherits the abstract class and then implements or provides the code for all the abstract methods declared in the abstract class by overriding them.
Thus it becomes compulsory to override the abstract method in the subclass. If the abstract method is not implemented in the subclass as well, then we have to declare the subclass also as “abstract”.
The general declaration of the abstract method is:
abstract void methodName (parameter_list);
While writing the abstract method, we need to remember the following rules:
- A class containing one or more abstract methods is an abstract class.
- Certain other keywords should not be used with the abstract keyword.
Advantages of Abstract Classes
- Abstract class in Java is highly beneficial in writing shorter codes.
- Abstraction in Java avoids code duplication.
- Abstract classes enable code reusability.
- Changes to internal code implementation are done without affecting classes.
Disadvantages of Abstract Classes
- Abstraction in Java is expensive, as sometimes you need to handle cases and situations which are not always necessary.
- Object-relational impedance mismatch, in case of RDBMS(Relational Data Base Management System).
- Object-relational mapping occurs in case of frameworks like hibernate.