How to Achieve Abstraction in Java With Example. By following the below method you can easily achieve Abstraction in Java. In Java programming, a class defined with the abstract keyword is said to be an abstract class. It can use both abstract and non-abstract methods within the body. If you have just marked your presence in the developing zone. Before jumping directly to the abstract class of Java, let’s explore the abstraction process first. Let's discuss how we achieve Abstraction in the Java language.
How to Achieve Abstraction in Java With Example
In Java, Abstraction can be achieved in two ways:
- By using abstract class
- By using interface
The interface helps you to achieve 100 per cent abstraction.
Abstract Class:
An abstract class is declared with the abstract keyword. It can leverage both abstract and non-abstract methods in it. An abstract class is extended and all its methods are implemented. Always remember a few rules before defining an abstract class in Java Programming.
- It must be declared by abstract keyword.
- It is comprised of both abstract and non-abstract methods.
- It can include constructors as well as static methods.
- It includes final methods which will not allow subclasses to alter the body methods.
Declaration:
abstract class X { }
Abstract Methods:
The methods which are defined with the abstract keyword without any implementation are called the abstract method. E.g.,
abstract void sayName(); //without method body
Real-world Example of Abstract Class
Let’s understand the concept of an Abstract class and methods with the help of an example. Here I’m defining an abstract class named office with abstract method salary. The implementation of office class is provided by the emp and othworkers classes.
Read: Different Types of Inheritance in Java With Example
For end users- these implementation classes will be hidden. The factory method (it returns the class instance) will provide the implementation class object. In this program, on creating the othworkers class instance, the salary() method will be invoked.
In the above example, the implementation will be unknown to end-users and the methods will be called by the user. The output of the program will be salaried workers.
Implementation of the interface can also be achieved by the abstract class. All the methods are overridden in such scenarios.
For end users- these implementation classes will be hidden. The factory method (it returns the class instance) will provide the implementation class object. In this program, on creating the othworkers class instance, the salary() method will be invoked.
abstract class office {
abstract void salary();
}
class emp extends office {
void salary() {
System.out.println(“salaried employees”);
}
class othworkers extends office {
void salary() {
System.out.println(“salaried workers”);
}
class AbstractionCheck {
public static void main(String args[]) {
office w = new otheworkers(); //object provided through method
w.salary();
}
}
In the above example, the implementation will be unknown to end-users and the methods will be called by the user. The output of the program will be salaried workers.
Another Example of Abstraction Achieved by Interface
Implementation of the interface can also be achieved by the abstract class. All the methods are overridden in such scenarios.
interface X {
void p();
void q();
void r();
}
abstract class Y implements X {
public void r() {
System.out.println(“Hello Its r”);
}
class S extends Y {
public void p() {
System.out.println(“Hi its p”);
}
public void q() {
System.out.println(“Hi its q”);
}
public void r() {
System.out.println(“Hi its r”);
}
class Check {
public static void main(String args[]) {
X obj = new S();
obj.p();
obj.q();
obj.r();
}
}
What is Abstraction Exactly in Java Programming?
In abstraction, we only show functionalities to a user by hiding the implementation and other details. Or, you can say displaying only useful information and hiding internal details. Let’s understand this with the help of an example, On your mobile phone, you set alarms without even knowing its internal functionalities like how it works.
You can join the Java Certification Course which will help you to understand the core basics of Java programming with real-world application development. Abstraction allows you to concentrate on objects' work rather than explaining how it does.
Output
Hi its p
Hi its q
Hi its r
Thus, we can see that abstraction can be achieved with the help of abstract classes as well as the interface. With the abstraction class, you can create transparency of 0 to 100 per cent while the interface allows you to achieve 100% abstraction.
0 Comments: