21/09/2018

How to Achieve Abstraction in Java Programming

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 discuss how we achieve Abstraction in the Java language.

abstraction in oop java

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.

Ways to Achieve Abstraction in Java:


In Java, Abstraction can be achieved in two ways:

  1. By using abstract class
  2. 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.

  1. It must be declared by abstract keyword. 
  2. It is comprised of both abstract and non-abstract methods. 
  3. It can include constructors as well as static methods. 
  4. 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: Inheritance in Java- Types and Applications

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, 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 for 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();
}
}

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. 
Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: