×

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

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

Race Condition in Java

Java is a multi-threaded programming language, race conditions are more likely to arise. Mostly because data can change when multiple threads visit the same resource simultaneously. Race conditions are concurrency...

3 minutes read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

Iccanobif Numbers in Java

In this article, you will be very well equipped with the knowledge of iccanobif numbers in Java. You will also know how they are formed and basic example programs on...

3 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 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.

Generics in Java

Generics in Java Parameterizedtypes mean generic. Generics allow types (Character, Integer, String, …, etc., as well as user-defined types) to act as parameters to interfaces, classes, and methods. Generics in Java...

9 minutes read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.

Fork Join in Java

Multithreaded processors are being introduced in new computer systems today. The operation is faster due to multicore CPUs. Therefore, it becomes essential for a programmer to leverage multithreaded processors effectively...

4 minutes read.

Java Tokens

Classes and methods are included in the Java program. The procedures also provide the expressions and statements required to finish a specific operation. Tokens make up the sentences and expressions...

4 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

Insertion Sort in Java

Insertion Sort in Java Insertion sort in Java is a simple sorting algorithm that works in the same way as we hold cards in hand. Insertion sort does the sorting element-by-element,...

3 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

Java AWT

Java AWT Java programming is used to develop different types of applications like window-based applications, web applications, Enterprise applications, or mobile applications. For creating standalone applications, Java AWT API is used....

11 minutes read.

Misc Operators in Java

In this article, we are going to learn about the misc operators in Java. Misc operators are nothing but the miscellaneous operators. The java programming language supports some of the...

4 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.