In object-oriented programming, the concept of abstraction is a cornerstone of efficiency and maintainability. Java, renowned for its elegant design, offers a robust framework for implementing abstraction, allowing developers to simplify complex systems, enhance code structure, and improve reusability. This article looks deep into the world of abstraction in Java, focusing on the pivotal role played by the Java abstract class and the mechanics of abstract methods.
By the end of this article, you will not only grasp the essence of abstraction but also be equipped to apply it effectively in your Java coding projects. If you are interested in upskilling yourself in this domain, we have listed a number of Java Certification Courses that you can pursue.
Abstraction in Java is the process of simplifying complex reality by modelling classes based on essential properties and behaviours. It involves defining the structure of a class while leaving the implementation details to its subclasses. This allows developers to create a blueprint for classes, ensuring consistency and efficiency in code design. Here is a code snippet to understand the concept:
abstract class Shape {
abstract void draw(); // Abstract method
}
class Circle extends Shape {
void draw() {
// Class creation to draw a circle
System.out.println("Drawing a circle");
}
}
Also Read:
A Java abstract class is a fundamental building block for abstraction. It is a class that cannot be instantiated and may have abstract methods, which are methods without a body. Abstract classes serve as a foundation for other classes and are designed to be extended by sub-classes. They encapsulate common attributes and behaviours while allowing for customisation by derived classes.
An abstract method in Java is a method declared without an implementation. These methods are meant to be defined in concrete subclasses, ensuring that each subclass provides its unique implementation. Abstract methods serve as a contract that derived classes must adhere to, enforcing consistency in the behaviour of the subclasses.
Also Read:
To achieve abstraction in Java, follow these steps:
Create an Abstract Class: Start by defining a Java abstract class that encapsulates common properties and behaviours.
Declare Abstract Methods: Within the abstract class, declare abstract methods that specify the methods subclasses must implement.
Extend the Abstract Class: Develop concrete subclasses that extend the abstract class and provide implementations for the abstract methods.
Instantiate Subclasses: Create instances of the concrete subclasses and use them in your code.
Let us look at an abstract Java class example to illustrate abstraction in Java using a Java abstract class:
// Define an abstract class
abstract class Shape {
String color;
Shape(String color) {
this.color = color;
}
// Declare an abstract method for calculating area
abstract double calculateArea();
}
// Create a concrete subclass
class Circle extends Shape {
double radius;
Circle(String color, double radius) {
super(color);
this.radius = radius;
}
// Implement the abstract method for circle area
double calculateArea() {
return Math.PI * radius * radius;
}
}
// Create another concrete subclass
class Rectangle extends Shape {
double length;
double width;
Rectangle(String color, double length, double width) {
super(color);
this.length = length;
this.width = width;
}
// Implement the abstract method for rectangle area
double calculateArea() {
return length * width;
}
}
public class AbstractionExample {
public static void main(String[] args) {
Circle circle = new Circle("Red", 5.0); //Creating a new Circle object
Rectangle rectangle = new Rectangle("Blue", 4.0, 6.0);
System.out.println("Circle Area: " + circle.calculateArea());
System.out.println("Rectangle Area: " + rectangle.calculateArea());
}
}
In this example, we define an abstract class ‘Shape’ with an abstract method ‘calculateArea()’. We create two concrete subclasses, ‘Circle’ and ‘Rectangle’, which implement the ‘calculateArea()’ method with their specific area calculation logic.
Data abstraction in Java focuses on hiding the internal details and showing only the necessary functionalities of an object. It simplifies complex data structures and operations, making them more accessible and understandable. Data abstraction is achieved through the use of classes, interfaces, and access modifiers.
Here is an abstraction in Java real time example, which demonstrates data abstraction in OOPs:
// Define an abstract class for Bank
abstract class Bank {
int accountNumber;
String accountHolder;
Bank(int accountNumber, String accountHolder) {
this.accountNumber = accountNumber;
this.accountHolder = accountHolder;
}
abstract void checkBalance();
void displayAccountInfo() {
System.out.println("Account Number: " + accountNumber);
System.out.println("Account Holder: " + accountHolder);
}
}
// Create a concrete subclass for a specific bank
class SBI extends Bank {
double balance;
SBI(int accountNumber, String accountHolder, double balance) {
super(accountNumber, accountHolder);
this.balance = balance;
}
void checkBalance() {
System.out.println("Available Balance in SBI: $" + balance);
}
}
public class BankExample {
public static void main(String[] args) {
SBI sbiAccount = new SBI(123456, "John Doe", 1000.0);
sbiAccount.displayAccountInfo();
sbiAccount.checkBalance();
}
}
In this abstract class program in Java, we have an abstract class ‘Bank’ with an abstract method ‘checkBalance()’. The concrete subclass ‘SBI’ extends ‘Bank’ and provides an implementation for ‘checkBalance()’. The ‘displayAccountInfo()’ method is inherited from the abstract class.
Also Read:
Define an Abstract Class: Create an abstract class that encapsulates common attributes and behaviours.
Declare Abstract Methods: Within the abstract class, declare abstract methods that specify behaviours to be implemented by subclasses.
Create Concrete Subclasses: Develop concrete subclasses that extend the abstract class and provide implementations for the abstract methods.
Instantiate Subclasses: Create instances of concrete subclasses and utilise their functionalities.
One common abstraction in Java real time example is the use of JDBC (Java Database Connectivity) to interact with databases. JDBC provides an abstract interface for connecting to different databases, and various database vendors implement this interface with their specific drivers. This abstraction allows developers to write database code without being tied to a particular database system.
Data abstraction is prevalent in everyday Java programming. A classic example is working with a List data structure. Developers use high-level List interfaces without needing to know the internal implementation details of specific List classes like ArrayList or LinkedList. This level of abstraction simplifies programming and promotes code reusability.
Abstraction in Java, achieved through abstract classes and abstract methods, is a powerful tool for simplifying complex systems and building modular, maintainable code. By defining essential attributes and behaviours in abstract classes, you can create a strong foundation for your application's architecture. As you explore the world of Java programming, remember that abstraction is a key concept that can lead to more efficient and scalable software.
Abstraction in Java is the process of simplifying complex systems by providing a high-level view of functionality while hiding implementation details. It is crucial for creating efficient and maintainable code.
A Java abstract class is a class that cannot be instantiated and may contain abstract methods. It serves as a blueprint for other classes and enforces consistency in derived classes.
Abstract methods in Java are methods declared without an implementation. They ensure that concrete subclasses provide their unique implementation, enforcing uniformity in behaviour.
To achieve abstraction in Java, you need to create an abstract class, declare abstract methods within it, develop concrete subclasses that implement those methods, and instantiate the subclasses.
One common real-world example of abstraction in Java is using JDBC to interact with databases. As for data abstraction, it is evident when working with high-level data structures like Lists, where implementation details are hidden from developers, promoting code reusability.
Application Date:11 November,2024 - 08 April,2025