All About Data Abstraction in Java

All About Data Abstraction in Java

7 mins read9.1K Views 1 Comment
Jaya
Jaya Sharma
Assistant Manager - Content
Updated on Oct 12, 2023 17:26 IST

Data abstraction in Java is the concept of hiding the internal details while only showing the functionality. This principle of object-oriented programming is implemented using abstract classes and methods.

2021_11_Abstraction-in-Java-1.jpg

Table of Contents

What is Data Abstraction in Java?

There are two types of abstraction in Java including control abstraction and data abstraction.

Control abstraction is used for building new functionalities and it also combines control statements in a single unit. This is a fundamental feature of every high-level language and forms the main unit of structured programming. This type of abstraction is used whenever we want to create a function for performing any given task.

Data Abstraction in Java is the process of reducing the object to its essence in order to only display the necessary characteristics to users. Abstraction defines an object in terms of attributes, methods and interfaces. In this article, we will be discussing the top interview questions based on data abstraction. 

We can achieve data abstraction through Abstract classes and interfaces. Through abstract classes, you can achieve partial or complete abstraction as abstract classes contain methods that have an implementation which can result in partial abstraction. On the other hand, interfaces allow complete (100%) abstraction since these allow users to abstract the implementation completely. In Java, data abstraction is implemented through abstract keywords while declaring a class or a method. 

Through data abstraction, the only essential information in a program is displayed without presenting the background details to the outside world. Other benefits include:

  1. Reducing complexity
  2. Increasing software reuse
  3. Avoiding Code Duplication
  4. Easing the burden of maintenance 
  5. Increasing Security and confidentiality

Data Abstraction in Java with Example Program

Here is an example of data abstraction in Java. The following example that is given above has a class Bus. In this class Bus, we have the abstract method to accelerate (). Then, we will inherit this class in the Volvo class. Within the Volvo class, we will implement the accelerate method.

This example will represent how an abstract class is defined, inherited, and used in the program.

 
abstract class Bus
{
abstract void accelerate();
}
class Volvo extends Bus
{
void accelerate()
{
System.out.println("Volvo::accelerate");
}
}
class Main
{
public static void main(String args[])
{
Bus obj = new Volvo(); //Bus object =>contents of Volvo
obj.accelerate(); //call the method
}
}
Copy code

Output:

Volvo::accelerate

Abstract Class

A class that has been declared as abstract is known as an abstract class. This abstract class is declared using an abstract keyword. It is a special type of class in Java that cannot be instantiated. Below is an example of an abstract class.

 
abstract class Car{
abstract void run();
}
class Hyundai extends Car{
void run(){System.out.println(β€œHyundai manufactures cars”);}
public static void main(String args[]){
Car obj = new Hyundai();
obj.run();
}
}
Copy code

For defining an abstract class, the below rules are to be followed:

  1. It should be declared with an abstract keyword.
  2. Both abstract and non-abstract methods are allowed.
  3. An abstract class cannot be instantiated.
  4. It can include static methods and constructors.
  5. It can have final methods.

We can create an object of subclass of an abstract class. Whenever we create an object of the subclass of an abstract class, it calls the constructor of that subclass. This constructor has a super keyword in the first line which calls the constructor of an abstract class. This is why constructors of an abstract class are used from the constructor of its subclass. A class extending the abstract class will not get compiled if the abstract class does not have a constructor.

We cannot instantiate an abstract class in Java since it is not complete. The abstract keyword is meant to ensure that no one can initiate this incomplete class. You cannot instantiate the abstract class by using a new operator. Such classes can be subclassed rather than getting instantiated. Abstract classes are used whenever the same method is to be shared with non-abstract subclasses that have specific requirements. 

Check out Learning To Program – Part 2: Abstractions

Difference between Abstract and Final Class

Final and abstract classes are opposite in nature. A final class cannot be modified further while an abstract class needs further modifications. β€œfinal” keyword is used for denoting that a class does not need any more improvements. On the other hand, the β€œabstract” keyword denotes that a class has the scope for further improvements.

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

– / –
6 weeks
β‚Ή15 K
3 months
β‚Ή2.05 K
3 months
– / –
170 hours
β‚Ή5.4 K
1 month
– / –
6 months
– / –
2 months
β‚Ή17 K
3 months
β‚Ή5.5 K
1 month

Difference between Data Encapsulation and Data Abstraction in Java

Encapsulation means wrapping up data within a single unit. It is a mechanism that binds together the code and data it manipulates. It protects the data from being accessed by using the code. In encapsulation, data or variables of a class are hidden from other classes. It can only be accessed through a member function of its own class in which they have been declared.

You can achieve Encapsulation by declaring all variables in the class as private and by writing public methods within the class to set and get values of variables. 

Data Abstraction is the property due to which non-essential units are not visible to the user. It is the process of identifying only the essential characteristics of an object while ignoring irrelevant details. 

Learn more about Object-Oriented Java: Inheritance and Encapsulation

Related Reads: Data Types in Java

FAQs

Q1. Differentiate between abstract class and concrete class.

There are two differences between an abstract and concrete class including: Objects of an abstract class cannot be created and only objects of its concrete subclasses can be created. There can be either zero or more abstract methods that are not allowed within a non-abstract class.

Define Abstract method in Java.

Another question that comes up while discussing data abstraction in Java is related to the abstract method. An abstract method is a method that is declared with an abstract modifier and has no implementation. It does not have a body and simply has a signature declaration which is followed by a semicolon. Its general form is as follows: Syntax:u00a0 u00a0u00a0u00a0u00a0u00a0abstract type MethodName(arguments); // No body An abstract method cannot be declared with the private modifier since it must be implemented within the child class. If it is declared as private, we cannot implement it from outside class.

Explain the Concrete method in Java?

A concrete method in Java is a method that is also known as the complete method in java. A concrete method can be overridden in the inherited class. If we declare this concrete method as "final" then it cannot be overridden and its implementation is complete. It is mandatory to override abstract methods within the subclass or else the subclass would be an abstract class and it will have to be declared as abstract.u00a0

What is the need to create reference to superclass?

It is important to create a reference to the superclass for accessing subclass features since superclass reference allows access to only those features of subclass that have already been declared in the superclass. It is not possible for superclass reference to access an individual method if you create the individual method in subclass.

What are the advantages of an abstract class within Java?

An abstract class makes the programming more flexible as it gives the scope for implementing abstract methods. Programmers can implement the abstract method for performing various tasks depending on the requirement.

Differentiate between an Interface and abstract class?

An abstract class has instance methods that support default behaviour. On the other hand, an interface does not allow the implementation of any default behaviour. An interface can declare various constants and instance methods. While an interface has all public members, an abstract class only contains class members such as private, protected, etc.

What will happen if all abstract methods are not overridden in a subclass?

We need to override all abstract methods in subclass or else the Java compiler will generate a compile-time error.u00a0

How do we define an Interface in Java? Ans:

In Java, an interface only defines the methods but does not implement them. It can include constants. Following is the syntax for defining an interface: public interface New { public void function One(); public long CONSTANT_ONE = 1000; }

Can an abstract class declare static methods in Java?

Yes, an abstract class can declare static methods but guidelines should be followed for making a method static in Java since it is not welcomed in the object-oriented design and static methods cannot be overridden within Java.

Can we create an instance of the abstract class?

No, it is not possible to create instances of an abstract class since an abstract class cannot be instantiated, meaning you cannot create instances.

Is it mandatory for abstract class to have abstract method?

No, it is not mandatory to have any abstract method. You can make an abstract class only through the use of abstract keyword within the class declaration. The compiler will then enforce every structural restriction to abstract class such as not allowing the creation of any instance.

Why is abstraction used in Java?

Abstraction is used for hiding unnecessary details from users. Data is selected from a larger pool to only display relevant details of the object to users so that the program becomes less complex.

About the Author
author-image
Jaya Sharma
Assistant Manager - Content

Jaya is a writer with an experience of over 5 years in content creation and marketing. Her writing style is versatile since she likes to write as per the requirement of the domain. She has worked on Technology, Fina... Read Full Bio

Comments

(1)

what is abstraction in Java .give briefly in interview preparation?

Reply to Penubaku Someswara

what is abstraction in Java .give briefly in interview preparation?