java
  1. java-abstraction

Java Abstraction

Java Abstraction is a fundamental concept in object-oriented programming. Abstraction is the process of hiding the implementation details and showing only the necessary information to the user. It is one of the essential mechanisms for achieving encapsulation, which is another crucial concept in object-oriented programming.

Syntax

To achieve abstraction in Java, we use abstract classes and interfaces.

public abstract class Animal {
    public abstract void makeSound();
}
public interface Shape {
    public double getArea();
}

Example

Let's take an example of an Animal class that has a method called makeSound. We can declare the Animal class as abstract to achieve abstraction, as shown below.

public abstract class Animal {
    public abstract void makeSound();
}

Now, we can create different types of animals like Cat, Dog, etc., and implement the makeSound method according to their sound.

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

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

The above code shows how abstraction can be achieved in Java through abstract classes.

Another way of achieving abstraction is through interfaces, as shown below.

public interface Shape {
    public double getArea();
}

Now, we can create different types of shapes like Circle, Rectangle, etc., and implement the getArea method according to their shape.

public class Circle implements Shape {
    private double radius;
  
    public Circle(double radius) {
        this.radius = radius;
    }
  
    public double getArea() {
        return Math.PI * radius * radius;
    }
}

public class Rectangle implements Shape {
    private double length;
    private double breadth;
  
    public Rectangle(double length, double breadth) {
        this.length = length;
        this.breadth = breadth;
    }
  
    public double getArea() {
        return length * breadth;
    }
}

The above code shows how abstraction can be achieved in Java through interfaces.

Output

If we create objects of the Cat and Dog classes and call their makeSound method as shown below, we get the following output.

Animal cat = new Cat();
cat.makeSound(); // Output: Meow

Animal dog = new Dog();
dog.makeSound(); // Output: Bark

Similarly, if we create objects of the Circle and Rectangle classes and call their getArea method as shown below, we get the following output.

Shape circle = new Circle(5);
System.out.println(circle.getArea()); // Output: 78.53981633974483
  
Shape rectangle = new Rectangle(5, 10);
System.out.println(rectangle.getArea()); // Output: 50.0

Explanation

Abstraction is the process of hiding the implementation details and showing only the necessary information to the user. In Java, we achieve abstraction through abstract classes and interfaces.

Abstract classes are classes that cannot be instantiated. They can have abstract methods as well as concrete methods. Abstract methods are methods that do not have an implementation. Any class that extends an abstract class must implement the abstract methods of that class.

Interfaces are similar to abstract classes, but they only contain public static final constants and public abstract methods. Any class that implements an interface must implement all the methods defined in the interface.

Use

Abstraction is used to hide the implementation details from the user and provide only the necessary information. It is used to achieve encapsulation, which is another crucial concept in object-oriented programming.

Abstraction is also used to define a common set of methods that different classes can implement in their own way. This helps in achieving code reusability and reduces code duplication.

Important Points

  • Abstraction is the process of hiding the implementation details and showing only the necessary information to the user.
  • In Java, we achieve abstraction through abstract classes and interfaces.
  • Abstract classes cannot be instantiated, and they can have abstract methods as well as concrete methods.
  • Interfaces only contain public static final constants and public abstract methods.
  • Abstraction is used to achieve encapsulation, code reusability and reduces code duplication.

Summary

Abstraction is one of the fundamental concepts in object-oriented programming. It is the process of hiding the implementation details and showing only the necessary information to the user. In Java, we achieve abstraction through abstract classes and interfaces. Abstraction is used to achieve encapsulation, code reusability and reduces code duplication.

Published on: