Abstraction in Java

3
3

Introduction

Abstraction means hiding internal implementation details and showing only the essential functionality of an object. Abstraction reduces complexity by allowing users to focus on what an object does instead of how it performs the operation.

For example, a person can drive a car using the steering wheel, brakes, and accelerator without understanding every internal part of the engine.

Abstraction

Abstraction

Features of Abstraction

  • Hides unnecessary implementation details

  • Shows only essential functionality

  • Reduces program complexity

  • Improves code organization

  • Makes implementation easier to modify

  • Allows different classes to implement the same operation differently

How Is Abstraction Achieved in Java?

Java mainly provides two ways to achieve abstraction:

  1. Abstract classes

  2. Interfaces

Abstract Class in Java

An abstract class is a class declared using the abstract keyword. It cannot be instantiated directly and is designed to be extended by other classes.

An abstract class can contain:

  • Abstract methods

  • Regular methods

  • Attributes

  • Constructors

  • Static methods

Abstract Method

An abstract method is declared without a method body. A concrete child class must provide its implementation.

abstract void start();

Abstract Class Example in Java

// Abstract class represents a general vehicle
abstract class Vehicle {
// Abstract method has no implementation
// Child classes must provide its implementation
abstract void start();
// Regular method contains a common implementation
void stop() {
System.out.println("Vehicle stopped.");
}
}
// Car extends the abstract Vehicle class
class Car extends Vehicle {
// Provide the implementation of the abstract method
@Override
void start() {
System.out.println("Car starts with a key.");
}
}
public class Main {
public static void main(String[] args) {
// Create a Car object using a Vehicle reference
Vehicle vehicle = new Car();
// Call the implemented abstract method
vehicle.start(); // Output :- Car starts with a key.
// Call the regular method inherited from Vehicle
vehicle.stop(); // Output :- Vehicle stopped.
// An abstract class cannot be instantiated directly
// Vehicle v = new Vehicle(); // Compilation error
}
}

In this example, Vehicle defines the required start() operation without explaining how a vehicle starts. The Car class provides the actual implementation.

Abstract method

Abstract method

Important Features of Abstract Classes

  • Declared using the abstract keyword

  • Cannot be instantiated directly

  • Can contain abstract and regular methods

  • Can contain attributes and constructors

  • Extended using the extends keyword

  • A class can extend only one abstract class

Interface in Java

An interface in Java defines a set of operations that implementing classes must provide. It specifies what a class should do without requiring one specific implementation.

An interface is declared using the interface keyword. A class uses the implements keyword to implement it.

interface InterfaceName {
void methodName();
}

Methods declared without a body in an interface are implicitly public and abstract.

Interface Example in Java

// Interface defines the required payment operation
interface Payment {
// Interface method has no implementation
void pay(double amount);
}
// CardPayment implements the Payment interface
class CardPayment implements Payment {
// Provide the implementation of the interface method
@Override
public void pay(double amount) {
System.out.println("Paid " + amount + " using card.");
}
}
public class Main {
public static void main(String[] args) {
// Create a CardPayment object using an interface reference
Payment payment = new CardPayment();
// Call the implemented interface method
payment.pay(500); // Output :- Paid 500.0 using card.
}
}

In this example, the Payment interface defines a pay() method. The CardPayment class decides how that payment operation is performed.

Interface

Interface

Important Features of Interfaces

  • Declared using the interface keyword

  • Cannot be instantiated directly

  • Implemented using the implements keyword

  • Defines common behaviour for implementing classes

  • A class can implement multiple interfaces

  • Implementing classes must provide the required method implementations

Abstract Class vs Interface

Abstract Class

Interface

Declared using abstract class

Declared using interface

Extended using extends

Implemented using implements

Can contain abstract and regular methods

Mainly defines behaviour that classes must implement

Can contain instance attributes

Fields are constants by default

Can have constructors

Cannot have constructors

A class can extend only one abstract class

A class can implement multiple interfaces

Suitable for closely related classes

Suitable for defining common behaviour across classes

Advantages of Abstraction

  • Reduced complexity: Unnecessary implementation details remain hidden.

  • Flexibility: Internal code can change without affecting its usage.

  • Reusability: Common behaviour can be shared through abstract classes.

  • Consistency: Interfaces can require multiple classes to follow the same structure.

  • Maintainability: Programs become easier to update and manage.

Abstraction vs Encapsulation

  • Abstraction hides implementation details and exposes essential behaviour.

  • Encapsulation protects data by restricting direct access to it.

Abstraction focuses on what an object does, while encapsulation focuses on protecting the object’s internal data.

Summary

Abstraction in Java hides internal implementation details and displays only essential functionality. Java achieves abstraction using abstract classes and interfaces.

Abstract classes can contain both abstract and regular methods, while interfaces mainly define operations that implementing classes must provide.

CS Core

Read Similar Blogs

Comments0