9/21/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, you can join the Java Certification Course which will help you to understand the core basics of Java programming with real-world application development. Before jumping directly to the abstract class of Java, let’s explore the abstraction process first. Let discuss how we to achieve Abstraction in Java language.

abstraction in oop java

What is the Abstraction exactly? 


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.

Abstraction allows you to concentrate on objects work rather than explaining how it does.

Ways to Achieve to 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 percent 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:

  1. It must be declared by abstract keyword. 
  2. It 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 as 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.

For end users- these implementation classes will be hidden. The factory mehtod (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 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 abstraction class, you can create transparency of 0 to 100 percent while interface allows you to achieve 100% abstraction. 

No comments:

Post a Comment