Java Abstraction

Abstraction in Java

Abstraction is a fundamental concept in Java programming that allows developers to create complex systems by simplifying them into manageable parts. In this tutorial, we will explore abstraction in Java, from its definition and principles to practical examples and real-world applications.

Definition and Purpose:

Abstraction is the process of simplifying complex systems by focusing on essential features while hiding irrelevant details. It enhances the clarity and efficiency of code.

Abstract Class

An abstract class is a class in Java that cannot be instantiated on its own. It serves as a blueprint for other classes and is often used to define common methods and fields that subclasses will inherit.

A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.

Key Characteristics: Abstract classes can contain both abstract methods (methods without a body) and concrete methods (methods with implementation). They are declared using the abstract keyword.

Declaring an Abstract Class

Syntax: To declare an abstract class, you use the abstract keyword followed by the class keyword, just like a regular class declaration.

Syntax
abstract class MyClassName{              }  

The important points to remember about abstract classes in Java as follows:

  • An abstract class must be declared with an abstract keyword.
  • It can have abstract and non-abstract methods.
  • Abstract classes cannot be instantiated directly.
  • Abstract classes can have constructors and static methods just like regular classes.
  • Abstract classes can also have final methods. When a method in an abstract class is declared as final, it means that subclasses cannot override or change the body of that method.
Example
// Abstract class definition
abstract class Animal {
    String name;

    // Constructor
    public Animal(String name) {
        this.name = name;
    }

    // Abstract method (no implementation)
    abstract void makeSound();

    // Concrete method
    void sleep() {
        System.out.println(name + " is sleeping.");
    }
}

// Subclass Dog
class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }

    // Implementing the abstract method
    void makeSound() {
        System.out.println(name + " says Woof!");
    }
}

// Subclass Cat
class Cat extends Animal {
    public Cat(String name) {
        super(name);
    }

    // Implementing the abstract method
    void makeSound() {
        System.out.println(name + " says Meow!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog("Buddy");
        Cat cat = new Cat("Whiskers");

        dog.makeSound(); // Output: Buddy says Woof!
        cat.makeSound(); // Output: Whiskers says Meow!

        dog.sleep(); // Output: Buddy is sleeping.
        cat.sleep(); // Output: Whiskers is sleeping.
    }
}

In this example:

  • We define an abstract class Animal with a constructor, an abstract method makeSound(), and a concrete method sleep().
  • We create two subclasses, Dog and Cat, which extend the Animal abstract class.
  • Each subclass provides an implementation for the abstract method makeSound().
  • In the Main class, we create instances of Dog and Cat and demonstrate how abstract methods and concrete methods work.

Abstract Methods

Abstract methods are methods declared in an abstract class without providing an implementation. Subclasses are required to implement these methods.

Let's provide a short and straightforward example to demonstrate how to implement abstract methods in subclasses.

Suppose we have an abstract class Shape with an abstract method area() that calculates the area of different shapes. We want to create concrete subclasses for specific shapes like Circle and Rectangle and provide implementations for the area() method.

Example
// Abstract class definition
abstract class Shape {
    // Abstract method to calculate area
    abstract double area();
}

// Subclass Circle
class Circle extends Shape {
    private double radius;

    // Constructor
    public Circle(double radius) {
        this.radius = radius;
    }

    // Implementing the abstract method
    double area() {
        return Math.PI * radius * radius;
    }
}

// Subclass Rectangle
class Rectangle extends Shape {
    private double length;
    private double width;

    // Constructor
    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    // Implementing the abstract method
    double area() {
        return length * width;
    }
}

public class Main {
    public static void main(String[] args) {
        Circle circle = new Circle(5.0);
        Rectangle rectangle = new Rectangle(4.0, 6.0);

        System.out.println("Circle Area: " + circle.area()); // Output: Circle Area: 78.53981633974483
        System.out.println("Rectangle Area: " + rectangle.area()); // Output: Rectangle Area: 24.0
    }
}

In this example:

  • We have an abstract class Shape with an abstract method area().
  • We create two concrete subclasses, Circle and Rectangle, which extend the Shape abstract class.
  • Each subclass provides its implementation of the area() method, calculating the area specific to that shape.

Why and When to Use Abstract Classes and Methods:

Purpose: Abstract classes and methods are used to hide complex implementation details and only reveal the essential parts of an object.

Why:

Security: They enhance security by allowing you to hide the inner workings of an object, exposing only what's necessary.

When:

  • Complexity: Use them when your object has complex behaviors or properties that you want to encapsulate.
  • Blueprint: When you want to create a blueprint for other classes to follow, ensuring a consistent structure.
  • Enforce Contracts: To enforce that subclasses provide specific behavior by implementing abstract methods.

Contact Us

Name
Email
Mobile No:
subject:
Message: