What is an Abstraction in Java and How to Implement It?

  • Written By  

  • Published on November 4th, 2022

Table of Contents [show]

Introduction

 

Java's Abstract Class just gives the user the information they need while concealing the intricate intricacies of the code implementation from them. The term "data abstraction" refers to this occurrence in object-oriented programming.

 

What is an abstract class?

In Java, an abstract class typically serves as a template for the methods and data members that will be utilised throughout the programme. Abstraction in Java prevents the user from looking at complex code implementations and provides the user with the necessary information.

Let's use a practical illustration to better comprehend abstraction. A automobile is the ideal illustration of abstraction. Don't we understand how an automobile operates and how it drives when we create one? But we are aware of how to make a car. It is crucial to infer the car rather than knowing how it operates. The same is an abstraction.

The same principle (as explained in the above example) is also used in Java and OOP programming. Only the necessary functionality is displayed or made available to the user in a programming language, hiding the implementation of the code from the user.

Properties of an abstract class

1. Template

An abstract class in Java enables the best way to perform the data abstraction process by providing developers with the ability to hide the code implementation. It also presents a template to the end user that explains the methods involved.

 

2. Free connection

Data abstraction in Java enables loose coupling by reducing dependencies at an exponential level.

 

3. Code reusability

Using an abstract class in your code saves time. We can call an abstract method wherever the method is necessary. An abstract class avoids the process of writing the same code again.

 

4. Abstraction

Data abstraction in Java helps developers hide code complications from the end user by reducing the complete characteristics of a project to only the necessary components.

 

5. Dynamic resolution

By supporting dynamic method resolution, developers can solve multiple problems using a single abstract method.

 

Our Learners Also Read: Sort Array in Java: All You Need to Know

 

Methods to achieve abstraction

•  We can achieve abstraction in two ways:

•  Using an abstract class
•  Using the interface

 

1. Using an abstract class

The same rules apply to Java classes that are abstract as well. The main distinction is that a conventional Java class does not employ abstract keywords while an abstract class does. To declare a class as abstract, use the abstract keyword before the class name.

 

Note that we cannot instantiate (create an object) an abstract class. An abstract class contains both abstract methods and concrete methods. We must inherit from the base class if we want to use an abstract class.

If a class does not have an implementation of all interface methods, we should declare the class as abstract. It provides complete abstraction. This implies that methods are empty and that fields are public, static, and final by default.
An abstract class's syntax is:

MainClass.java

 

1.    //abstract class  
2.    abstract class Demo  
3.    {  
4.    //abstract method  
5.    abstract void display();  
6.    }  
7.    //extends the abstract class  
8.    public class MainClass extends Demo  
9.    {  
10.    //defining the body of the method of the abstract class  
11.    void display()  
12.    {  
13.    System.out.println("Abstract method called.");  
14.    }  
15.    public static void main(String[] args)  
16.    {    
17.    MainClass obj = new MainClass ();  
18.    //invoking abstract method  
19.    obj.display();  
20.    }  
21.    }  

Output:

Abstract method called.

 

2. Using the interface

An interface in Java is comparable to a Java class. The distinction is that variables and empty methods (methods without method implementations) are present in an interface. In other terms, it is a set of static constants and abstract methods (methods without a method body). The fact that each method is public, abstract, and devoid of a function Object() { [native code] } is a crucial aspect of an interface. Along with abstraction, it also helps achieve multiple inheritances. Implementation of these methods is provided by clients when implementing the interface.

Note: We can achieve 100% abstraction using interfaces.
Separating the interface from the implementation is one way to achieve abstraction. The Collection framework is a great example of this.

 

Car.java

 

1.    interface CarStart  
2.    {    
3.    void start();    
4.    }    
5.    interface CarStop  
6.    {    
7.    void stop();    
8.    }    
9.    public class Car implements CarStart, CarStop  
10.    {    
11.    public void start()  
12.    {  
13.    System.out.println("The car engine has been started.");  
14.    }    
15.    public void stop()  
16.    {  
17.    System.out.println("The car engine has been stopped.");  
18.    }    
19.    public static void main(String args[])  
20.    {    
21.    Car c = new Car();    
22.    c.start();    
23.    c.stop();    
24.    }  
25.    }

  
Output:

The car engine has been started.
The car engine has been stopped.

 

Interface Features

•  We can achieve complete abstraction.
•  We can use multiple interfaces in a class which leads to multiple inheritances.
•  It also helps to achieve a free connection.

Syntax:

 

public interface XYZ
{
method public void();
}
Java has a keyword called implements to use an interface in a class. We provide the necessary implementation of the method we declared in the interface.

 

Advantages of abstract classes

•  Abstract class in Java is very beneficial in writing shorter codes
•  Abstraction in Java prevents code duplication
•  Abstract classes allow code reuse
•  Changes to the internal code implementation are made without affecting the classes.

 

Disadvantages of abstract classes

•  Abstraction in Java is expensive because sometimes you need to handle cases and situations that are not always necessary.
•  Object-relational impedance mismatch in the case of RDBMS
•  Object-relational mapping occurs in the case of frameworks such as Hibernate.

 

Conclusion

Abstraction in Java is very effective at helping you limit what you share with your users, and you can choose the level of abstraction you want to deploy in your application. In abstract classes, you can use concrete methods to achieve partial abstraction and interfaces to achieve full abstraction.

Abstract methods are necessary when you leave the task of determining the implementation of a child class.
 

 

About The Author:

logo

Digital Marketing Course

₹ 29,499/-Included 18% GST

Buy Course
  • Overview of Digital Marketing
  • SEO Basic Concepts
  • SMM and PPC Basics
  • Content and Email Marketing
  • Website Design
  • Free Certification

₹ 41,299/-Included 18% GST

Buy Course
  • Fundamentals of Digital Marketing
  • Core SEO, SMM, and SMO
  • Google Ads and Meta Ads
  • ORM & Content Marketing
  • 3 Month Internship
  • Free Certification
Trusted By
client icon trust pilot