Java Abstraction
Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process for hiding the implementation details from the user. Java developers can only provide the functionality to the user and hide the implementation detail using abstraction. Abstraction is one of the most important features of oops. It is achieved using abstract classes and interfaces. Java uses a keyword 'abstract’ to make a class abstract. Suppose a user is making a call to another user. He is simply enjoying the service, but he has no idea about the functionality of how things are working, this is called an Abstraction. Let's understand it with a programming example. Suppose you are making a banking application and you are asked to collect all the information of the customer. Then there is a chance that you will come up with some additional info about the user like favorite movie, favorite Actor, but all these information are not necessary to create an application. So you need to select only the useful information for your project from that collection of data. Since we can fetch/ remove/ select the information from that collection of data, this process is referred to Abstraction.
How to achieve Abstraction in Java?
There is two way to achieve abstraction in Java.- Java Abstract classes (0 to 100%)
- Java Interfaces (100%)
abstract class Fortuner{ abstract void speed(); } class Car extends Fortuner{ void speed(){ System.out.println("Speed is 220 km/h");} public static void main(String args[]){ Fortuner obj=new Car(); obj.speed(); }Output
Speed is 220 km/hAbove example describes that how the abstraction achieved in java. Class Fortuner and method speed() is abstracted in the given example.
Java Abstract classes
An Abstract class is a class that contains abstract as well as concrete methods. A class is created Abstract using the keyword 'abstract’. An Abstract class can't be instantiated, in other words. We cannot create the object of an abstract class. Abstract class can have both abstract and concrete methods. abstract class Account{ public abstract void debit(); public void credit() { System.out.println(“Account Statement”)}} If we create the object of the given class Account then it will produce an error. Account obj=new Account(); //it will produce an error To use the abstract class, we must inherit it from the other class. Abstract methods in abstract class do not have a body. We can achieve 0 to 100 percent abstraction using Java abstract classes. See the Example below//Abstract class abstract class Transaction { public abstract void credit(); public void debit() { System.out.println("Account is debited"); } } //Subclass (inherit from Transaction) class Account extends Transaction{ public void credit() { // The body of credit() is provided here System.out.println("Account is credited"); } public static void main(String[] args) { Account obj = new Account(); obj.credit(); obj.debit(); } }Output
Account is credited Account is debited
Java Interface
Java interface is a collection of abstract methods and static constants. It’s like a class that contains only static constants and abstract methods. We can achieve 100 % abstraction using Interfaces. The interface does not contain any constructors. Methods in an interface are only public and abstract. The interface is responsible for what a class will do; it is like a blueprint of the class. Java uses interfaces to achieve multiple inheritances. To use an interface in a class, use the keyword implements after the interface name. Java class can implement more than one interface (multiple interfaces). Let's take a tour with the following examples to understand the implementation of the interface. Example 1 This is a format of the interface.public interface InterfaceName { public String hello = "Hello"; public void sayHello(); }As you can see at the given example, an interface is declared using the interface keyword. Just like with classes, a Java interface can be declared public or package scope; it does not use access specifier. The above example contains one variable and one method. In Java, the variable can be accessed directly from the interface, like this:
System.out.println(InterfaceName.hello);Example 2
interface X { void m1(); void m2(); void m3(); void m4();} abstract class Test2 implements X{ public void m3() { System.out.println("In method m3"); }} class Test3 extends Test2{ public void m1(){System.out.println("In method m1");} public void m2(){System.out.println("In method m2");} public void m4(){ System.out.println("In method m4"); }} public class Test1{ public static void main(String args[]){ X obj=new Test3(); obj.m1(); obj.m2(); obj.m3(); obj.m4();} }Output
In method m1 In method m2 In method m3 In method m4
Advantage of Abstraction
Some of the advantages of abstraction are as follows- It reduces view complexity of the class members.
- It avoids duplication of code.
- It increases the reusability of code.
- It Increases the readability of the program if things are appropriately abstracted, then the reader does not retain everything.
- It supports polymorphism.
- It provides security.
Abstraction VS. Encapsulation
Both abstraction and interface are a major oops concept of Java. They allowed you to enhance the real world thing into an object so it can be implemented in the program. Many are got confused between abstraction and encapsulation because they are quite similar. Abstraction hides complexity while encapsulation hides internal working. Let’s have a look at some following differences.1 | Abstraction hides details at the design level. | Encapsulation hides details at the implementation level. |
2 | Abstraction hides unwanted information for the user's dynamic view. | Encapsulation hides the block of code. Wrap it into a single unit. |
3 | It uses the keyword abstract to make an abstract class for abstraction | In encapsulation create a class variable as private. |
4 | Abstraction is created using abstract classes and interfaces. | Provides setter and getter for accessing and modifying the data. |
5 | Abstraction is used for hiding the backend data. | It wraps up data into a single unit. |