In Java, abstraction ensures that users only have access to important details. Non-essential and trivial units are not displayed to the user. Dive into this article to learn more about data abstraction in Java.
What is Abstraction in Java?
Abstraction in Java can be described as the process of spotting the required features of an object while ignoring the irrelevant details. The properties and behaviors of an object separate it from other objects of the same type. They also help with the classification or grouping of objects.
Example of Abstraction in Java
Take a look at one abstraction in Java example:
// Java program to illustrate the
// concept of Abstraction
abstract class Shape {
String color;
// these are abstract methods
abstract double area();
public abstract String toString();
// abstract class can have the constructor
public Shape(String color)
{
System.out.println("Shape constructor called");
this.color = color;
}
// this is a concrete method
public String getColor() { return color; }
}
class Circle extends Shape {
double radius;
public Circle(String color, double radius)
{
// calling Shape constructor
super(color);
System.out.println("Circle constructor called");
this.radius = radius;
}
@Override double area()
{
return Math.PI * Math.pow(radius, 2);
}
@Override public String toString()
{
return "Circle color is " + super.getColor()
+ "and area is : " + area();
}
}
class Rectangle extends Shape {
double length;
double width;
public Rectangle(String color, double length,
double width)
{
// calling Shape constructor
super(color);
System.out.println("Rectangle constructor called");
this.length = length;
this.width = width;
}
@Override double area() { return length * width; }
@Override public String toString()
{
return "Rectangle color is " + super.getColor()
+ "and area is : " + area();
}
}
public class Test {
public static void main(String[] args)
{
Shape s1 = new Circle("Red", 2.2);
Shape s2 = new Rectangle("Yellow", 2, 4);
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
Output:
Shape constructor called
Circle constructor called
Shape constructor called
Rectangle constructor called
Circle color is Redand area is : 15.205308443374602
Rectangle color is Yellowand area is : 8.0
An abstract class in Java contains the abstract keyword in its declaration. Abstract classes may or may not include an abstract method in Java. If a class comes with at least one abstract method, you will have to declare the class abstract.
A class cannot be instantiated if a class is declared abstract. If you want to use an abstract class, you should be able to inherit it from another class. You will be required to inherit an abstract class from another class to use it. Once you inherit an abstract class in Java, you will be required to provide implementations to all abstract methods within it.
Advantages of Using Abstraction in Java Programming
The different advantages of using abstraction in Java programming are as follows:
Abstraction can reduce the complexity of viewing things in Java.
Java abstraction can increase code reusability by preventing code duplication.
Java abstraction can help increase the security of a program or application by making only essential details accessible to users.
Java abstraction can help with improving the maintainability of an application.
Abstraction is useful for enhancing the modularity of the application.
Improvements become easier because you will be able to perform various modifications in the internal system without affecting end-users.
Java abstraction can hide implementation details and display only relevant information.
It ensures that your users get a simple and clear interface.
Abstraction maximizes security by restricting access to internal class details.
Abstraction supports modularity because complex systems can be segmented into smaller and more manageable parts.
Java abstraction can hide implementation complexity details from the user, making the code easier to use and understand.
Abstraction supports a lot of flexibility in terms of implementing a program. Adjustments to the underlying implementation details can be completed without impacting the user-facing interface.
Java abstraction allows separation and modularity of concerns. It makes code more maintainable and easier to debug.
What is an Abstract Method and Its Implementation?
If you want a class to include a specific method, but you want the original implementation of that method to be specified by child classes, you will have to declare the method in the parent class as an abstract.
One example of the abstract method in Java is as follows:
abstract void printStatus();//no method body and abstract
Introduction to Interfaces and Their Purpose in Abstraction
Interfaces are one of the most powerful tools to attain abstraction in Java. Since interfaces are like abstract classes, they cannot be instantiated. As they don’t provide an implementation, you won’t have a constructor either.
Defining Interfaces and their Syntax in Java
The syntax for the abstract class and the interface is as follows:
public interface Hittable {
int calculateDamage(int strength, double velocity, double angle);
}
Example of Abstract Class That Has an Abstract Method
One abstract class in Java example with an abstract method is as follows: //Example of an abstract class that has abstract and non-abstract methods abstract class Bike{ Bike(){System.out.println(“bike is created”);} abstract void run(); void changeGear(){System.out.println(“gear changed”);} } //Creating a Child class which inherits Abstract class class Honda extends Bike{ void run(){System.out.println(“running safely..”);} } //Creating a Test class which calls abstract and non-abstract methods class TestAbstraction2{ public static void main(String args[]){ Bike obj = new Honda(); obj.run(); obj.changeGear(); } }
You will be able to create abstract classes and methods with the help of the abstract keyword. An abstract method does not come with any implementation. Any class containing abstract methods also needs to be abstract. But you won’t be able to create objects using an abstract class.
If you want to implement features of an abstract class, you will have to inherit the subclass from it and create objects of the subclass. A subclass should be able to override all abstract methods in an abstract class.
FAQs
How does abstraction simplify code and promote code reusability?
Abstraction in Java can simplify code by breaking it down into smaller and easily manageable components. Abstraction enables developers to create modular and reusable code that can be used in various contexts.
What are the benefits of using abstraction in Java programming?
The use of abstraction in Java programming is extremely useful for writing shorter codes. Moreover, abstraction classes support code reusability and also prevent code duplication. The abstract class in Java are also useful for changing the internal code without impacting classes.
When to use Abstract Methods & Abstract Class?
You should use abstract methods and abstract classes for sharing code among several closely related classes.
What are the 2 types of abstraction in Java?
The two types of abstraction in Java are data abstraction and process abstraction.
What are some best practices for effectively applying abstraction in Java projects?
You can use an abstract class or an interface to apply abstraction in Java projects.
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.