Skip to main content

Abstraction in Java

  • Abstraction is the process of hiding the implementation details and showing only the functionality to the user.

  • It helps in reducing complexity by allowing the user to interact with an object at a higher level without needing to know the internal workings.

  • It allows us to focus on what an object does instead of how it does it.

Key Characteristics of Abstraction

  • Simplification: It simplifies complex systems by breaking them down into smaller, more manageable parts.

  • Focus on Essentials: It allows us to focus on what an object does rather than how it does it.

  • Reduce Complexity: It reduces the complexity of the code by hiding unnecessary details.

  • Improved Code Readability: It makes the code easier to read and maintain.

Abstraction in Java

  • In Java, abstraction is achieved using abstract classes.

  • An abstract class is a class that cannot be instantiated.

  • It can have abstract methods (methods without a body) as well as concrete methods (methods with a body).

  • Abstract classes are meant to be subclassed, and their abstract methods should be implemented by the subclasses.

Abstract Class

  • An abstract class in Java is declared using the abstract keyword.

  • It may contain both abstract methods (without a body) and concrete methods (with a body).

  • Abstract classes cannot be instantiated directly.

Syntax of Abstract Class

abstract class AbstractClass {
// Abstract method (does not have a body)
abstract void abstractMethod();

// Concrete method
void concreteMethod() {
System.out.println("This is a concrete method.");
}
}

Example of Abstract Class

Let's look at an example to understand how abstract classes work in Java.

abstract class Shape {
// Abstract method
abstract void draw();

// Concrete method
void display() {
System.out.println("Displaying shape");
}
}

class Circle extends Shape {
// Implementing the abstract method
@Override
void draw() {
System.out.println("Drawing a Circle");
}
}

class Rectangle extends Shape {
// Implementing the abstract method
@Override
void draw() {
System.out.println("Drawing a Rectangle");
}
}

public class Main {
public static void main(String[] args) {
// Creating objects of Circle and Rectangle
Shape circle = new Circle();
Shape rectangle = new Rectangle();

// Calling methods
circle.display(); // Outputs: Displaying shape
circle.draw(); // Outputs: Drawing a Circle

rectangle.display(); // Outputs: Displaying shape
rectangle.draw(); // Outputs: Drawing a Rectangle
}
}

Explanation:

  • Abstract Class (Shape): Contains an abstract method draw() and a concrete method display().

  • Concrete Classes (Circle and Rectangle): Extend the Shape class and provide implementations for the draw() method.

  • Using the Abstract Class: Objects of Circle and Rectangle are created, and both abstract and concrete methods are called.

Benefits of Using Abstract Classes

  • Code Reusability: Abstract classes allow code to be reused across multiple classes.

  • Enforce Implementation: Abstract methods in abstract classes ensure that subclasses provide specific implementations for the required methods.

  • Provide Default Behavior: Abstract classes can provide default behavior for some methods, which can be inherited by subclasses.

Abstraction in Action: Real-world Example

Consider a scenario where we have an abstract class Animal with an abstract method makeSound(), and two classes Dog and Cat that extend this abstract class.

abstract class Animal {
// Abstract method
abstract void makeSound();

// Concrete method
void sleep() {
System.out.println("Sleeping...");
}
}

class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Bark");
}
}

class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Meow");
}
}

public class Main {
public static void main(String[] args) {
// Creating objects of Dog and Cat
Animal dog = new Dog();
Animal cat = new Cat();

// Calling methods
dog.makeSound(); // Outputs: Bark
dog.sleep(); // Outputs: Sleeping...

cat.makeSound(); // Outputs: Meow
cat.sleep(); // Outputs: Sleeping...
}
}

Explanation:

  • Abstract Class (Animal): Contains an abstract method makeSound() and a concrete method sleep().

  • Concrete Classes (Dog and Cat): Extend the Animal class and provide implementations for the makeSound() method.

  • Using the Abstract Class: Objects of Dog and Cat are created, and both abstract and concrete methods are called.

Summary

  • Abstraction: The process of hiding implementation details and showing only the functionality to the user.

  • Abstract Class: A class that cannot be instantiated and can have abstract and concrete methods. Subclasses are required to implement the abstract methods.

  • Benefits: Abstraction simplifies complex systems, reduces code complexity, improves code readability, and enhances maintainability.

Understanding abstraction is crucial for designing robust and maintainable object-oriented code in Java.