×

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%)
Example This is an example of Java Abstraction.
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/h
Above 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.

Related Topics

Interchange Diagonal elements in Java

In this article, you will be very well acknowledged about what is a matrix with an example. You will also be acknowledged about how to interchange the diagonal elements in...

4 minutes read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

How to Convert Object to String in Java

How to Convert Object to String in Java You can convert any Object to String in Java whether it is a user-defined class, StringBuilder or StringBuffer, etc. There are two methods...

2 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

How many ways to create object in Java?

In this article, you will be acknowledged about the different ways to create an object in java. So far you construct an object from a class, as is common knowledge,...

6 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

Java Swing

Java Swing is the extension of Abstract Windows Toolkit (AWT). Any component designed in Swing will appear the same on any platform. Problems/Disadvantage of Abstract Windows Toolkit (AWT): As we know that...

27 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Dutch National Flag Problem in Java

Dutch National Flag (DNF) is a programming issue that Edsger Dijkstra put up. The white, red, and blue hues make up the Dutch flag. The goal is to haphazardly set...

6 minutes read.

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

Check the presence of Substring in a String in java

In java, the string can be treated as class and datatype. The string contains words and numbers but should be in double-quotes. Example: ” Omsairam” Substring The part of the string is called...

2 minutes read.

How to Import Packages in Java

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Java vs Golang

Java Java is both a programming language and a platform. Java is a high-level, dependable, object-oriented, and secure programming language. Java was developed in 1995 by Sun Microsystems, which is now an...

4 minutes read.

Advantages of Generics in Java

Generic offers a variety of benefits. The programmer's life is made easier by using generic Java. In this section, we are going to discuss about Java's generic’s and its benefits. 1....

4 minutes read.

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Model Class in Java

In this section, we will be acknowledged about the model class in Java, its purpose and its uses. Also, we will learn how is this created and leveraged in java. Model...

4 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers. Moran Number A moran numbers are...

3 minutes read.

Rehashing in Java

The most crucial idea mostly in the data structure is hashing, which is utilized to change a particular key into some other value. The hash function can be used to...

7 minutes read.