Java, as a programming language, follows almost all of the object oriented concepts. One of the main pillars of any object oriented language is "Abstraction". Java helps us implement this concept with the help of different interfaces and classes
Let us explore further on what abstraction is and how java implements these concepts.
"Abstraction of data is a property in which only the required details are displayed to the user. The remaining details that are trivial and not required are made sure to not reach the user."
There are two types of Abstraction namely,
Data Abstraction:In this type of abstraction we hide the complex details by creating different data types. We only show the required details to the user.
Control Abstraction: This abstraction gathers all the control statements that are present in the program and shows them as a package.This forms the most important part of any structured programming/
Example 1: The most common example given to explain abstraction is that of a car and its working.
Imagine a person driving a car. The controls he/she has are the accelarator, brakes and clutch. He/She knows when there's an obstacle he/she has to apply the brakes. But he/she isn't aware of how the brakes work. If we think about it, we aren't aware of anything about how the car works internally until we study about it in detail. This is the basic idea of Abstraction.
Example 2: Another example which can explain abstraction is that of a restaurant. Imagine going to your favourite Restaurant and ordering your favourite food. Most of the times, at restaurants, the making of the food isn't shown to the customers. But, the food is made available to us. This is another illustration of abstraction where the preparation of the food isn't shown to us but the food is provided to us.
Java consists of a non-access modifier called "abstract" that can be used to implement abstraction. Classes and methods can both utilise this abstract modification, but variables cannot.
Complete abstraction is provided by the interface, which means that it only offers method prototypes—not their implementation. A partial abstraction is offered by an abstract class, wherein at least one method is not required to be implemented.
In the above code snippet, we have a class Food. The class food consists of a Order method. This class is inherited by the Pizza class. In this Pizza class we implement the Order method. The abstract class is first defined, then inherited and then implemented in the program.
A class that has the keyword "abstract" in its declaration and that cannot be instantiated is said to be an Abstract Class in an Object Oriented Programming construct. These classes contain the abstract methods. Abstract methods are methods which have just the definitions of the method but not implementation.
There may or may not be any abstract methods in an abstract class (a method that has no implementation).
We can create an abstract class using the syntax shown below.
1. The fundamental advantage of employing abstraction in programming is that it enables you to assemble a number of related classes as siblings.
2. Abstraction is one of the best feature of Object Oriented programming which helps us to reduce the complexity of a product and makes the software implementation quite easy.
3. The code is dispersed over numerous files and is no longer with in analyst's brain. The fact that people can understand what is happening in their personal language gives businesses more confidence.
Abstract classes are mostly used when many subclasses perform the same operation in various methods through different ways of implementation. Additionally it extends the same abstract class and provides many abstract method implementations.
The class hierarchy used in Object Oriented Programming is explained using Abstract Classes. In order to provide the abstract class's implementation details, it also describes subclasses. This implementation is shown below in the code snippet.
In order to give the user with simply the external perspective of a system that has common techniques or operations to describe, we generally choose abstraction. Therefore, we remove the common methods, abstractly express them, and group these abstract methods into a common abstract class. Following the representation of a system's general structure as an abstract class and all the general operations as abstract methods, we are then able to derive any number of classes from the provided abstract class and override the abstract methods to perform these operations for each class. Designing a system becomes valuable in this approach.
The abstract class is shown above. Interfaces are yet another set of construction elements that support abstraction. Interfaces represent a contract and classes that implement interfaces must uphold the contracts.
Interface contracts are nothing more than unimplemented methods. Interfaces consists of just the prototypes of the methods. No single implementation of any methods will exist inside an interfaces.
Any class implementing the MyInterface class has to override the 'myMethod' method.
|Can have abstract or normal methods||Can have only abstract methods|
|Can have final or non-final, static or non-static variables.||Can have final and static variables|
|Inherited using 'extends' keyword||Implemented using 'implements' keyword.|
|May provide the implementation of Interface||Can not provide the implementation of Abstract Class|
|can extend many classes or implement other interfaces||Can only implement an interface|
Encapsulation's purpose is to ensure that "sensitive" data is obscured from users. In order to do this, one must declare the methods and datamembers of the class as private or provide a get method or a set method to modify the value of a variable which is private.
The reasons for using Encapsulation is to get a better control of data members and methods of the class. The class is flexible as it helps the programmer change the required part of code without disturbing the whole code. Security of data is more. The below code snippet implements this feature.
As abstraction and encapsulation offer similar services, they can often be a confusion. Let us understand the difference between these to important concepts of OOPs.
|It is the process of accesing and abstracting the information||It is the process of binding the information|
|used in Interface or design level.||used in the Implementation level.|
|Hides details||Binds the details into one package.|
|Usage of abstract class||Usage of access modifiers|
|In abstraction, only the required view is shown to the user and the other details are hidden.||In encapsulation, data is bound into a unit and it can be secured using access modifiers.|
This concludes our discussion of "Abstraction in Java". I hope that you gained knowledge about this amazing feature offered by java and are now more interested to dive into the language. You can consult the Java Tutorial for further guidance.
Good luck and happy learning!