Abstraction in Java [Detailed Explanation]

Abstraction in Java [Detailed Explanation]

In this article, we'll learn and discuss about the Abstraction property along with their detailed approach using Java. Let's start!

In object-oriented programming, abstraction is the process of hiding the implementation details of an object and only showing the essential characteristics and behaviors of the object. This allows for the creation of simplified, reusable code, as well as the ability to change the implementation of an object without affecting the code that uses it. Abstraction can be achieved through the use of interfaces, abstract classes, and encapsulation.

How can we achieve Abstraction?

There are several methods of achieving abstraction in object-oriented programming, including:

  1. Abstract classes: An abstract class is a class that cannot be instantiated, but can be inherited by other classes. Abstract classes can have abstract methods, which are methods without a body that must be implemented by the child class.

  2. Interfaces: An interface is a contract that specifies a set of method signatures that a class must implement. Interfaces do not provide any implementation details and are used to define a common set of behaviors that can be implemented by multiple classes.

  3. Encapsulation: Encapsulation is the practice of hiding the implementation details of an object and only exposing its public interface. This allows for the internal workings of an object to change without affecting the code that uses it.

  4. Polymorphism: Polymorphism is the ability of an object to take on multiple forms. This allows for objects of different classes to be used interchangeably, as long as they implement the same methods or interfaces.

  5. Data hiding: Hiding the data members of a class from other classes and providing public methods to access or manipulate the data. This ensures that the internal state of an object cannot be directly accessed or modified by external code, which helps to maintain the integrity of the object's state.

Abstract Classes

In Java, an abstract class is a class that cannot be instantiated but can be inherited by other classes. An abstract class is defined using the keyword "abstract" before the class keyword. An abstract class can have both abstract and non-abstract methods.

Here is an example of an abstract class in Java:

abstract class Shape {
    // abstract method
    abstract void draw();
    // non-abstract method
    void displayArea() {
        System.out.println("Area of shape: ");
    }
}

In this example, the Shape class is an abstract class that contains an abstract method draw() and a non-abstract method displayArea(). The draw() method doesn't have a body and must be implemented by the child class.

Here is an example of a child class that inherits from the Shape class and implements the draw() method:

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a circle");
    }
}

In this example, the Circle class inherits from the Shape class and implements the draw() method. The Circle class can also use the displayArea() method from the Shape class.

And we can create an object of Circle class and call the draw method on it :

Circle circle = new Circle();
circle.draw();

This will print Drawing a circle on the console.

An important point to note is that we cannot create an object of an abstract class, but we can create a reference variable of an abstract class.

In summary, an abstract class is a class that cannot be instantiated but can be inherited by other classes. An abstract class can have both abstract and non-abstract methods, abstract methods must be implemented by the child class.

Interface

In Java, an interface is a contract that specifies a set of method signatures that a class must implement. An interface is defined using the keyword "interface" and contains only abstract methods, which do not have a body.

Here is an example of an interface in Java:

interface Shape {
    void draw();
    void displayArea();
}

In this example, the Shape interface defines two abstract methods, draw() and displayArea() that must be implemented by a class that implements this interface.

Here is an example of a class that implements the Shape interface:

class Circle implements Shape {
    public void draw() {
        System.out.println("Drawing a circle");
    }
    public void displayArea() {
        System.out.println("Calculating area of Circle");
    }
}

In this example, the Circle class implements the Shape interface and provides an implementation for the draw() and displayArea() methods.

It is also possible for a class to implement multiple interfaces. For example:

class Circle implements Shape,AnotherShape{
    public void draw() {
        System.out.println("Drawing a circle");
    }
    public void displayArea() {
        System.out.println("Calculating area of Circle");
    }
    public void displayPerimeter(){
        System.out.println("Calculating perimeter of Circle");
    }
}

In this case, Circle class implements two interfaces Shape and AnotherShape and thus provides implementation for all the methods present in those interfaces.

It's worth noting that, in Java, an interface cannot be instantiated and only a class that implements an interface can create an instance.

In summary, an interface in Java is a contract that specifies a set of method signatures that a class must implement. An interface can only contain abstract methods, that is methods without a body, and a class that implements an interface must provide an implementation for all the methods defined in the interface. A class can also implement multiple interfaces.

Abstract Classes vs Interface

  1. Abstract classes can have both abstract and non-abstract methods, while interfaces can only have abstract methods.

  2. An abstract class can have constructors, while an interface cannot.

  3. A class can only extend one abstract class, but it can implement multiple interfaces.

  4. An abstract class can provide a default implementation of an interface, while an interface can only declare the method signature.

  5. An abstract class can have instance variables and a constructor, while an interface cannot.

  6. An abstract class can be used to provide a common interface for different implementations of a component, while an interface is used to define a contract for behavior that must be implemented by any class that implements the interface.

  7. An abstract class can have a constructor and non-final variables, while an interface cannot.

  8. An abstract class can have a protected or private method, which interface cannot have.

  9. An abstract class can have a main method, while an interface cannot.

  10. An abstract class is used when there is a common behavior among the subclasses, while an interface is used to enforce a common behavior among unrelated classes.

In summary, abstract classes and interfaces are both used for abstraction in object-oriented programming, but they have some important differences. Abstract classes can have both abstract and non-abstract methods, while interfaces can only have abstract methods. A class can only extend one abstract class, but it can implement multiple interfaces. Abstract classes can provide a default implementation of an interface, while interfaces can only declare the method signature.

Advantages of Abstraction

Abstraction in object-oriented programming has several advantages, including:

  1. Code Reusability: Abstraction allows for the creation of reusable code, which can be used in multiple parts of a program. This reduces the amount of duplicated code and makes it easier to maintain and update the program.

  2. Modularity: Abstraction allows for the separation of concerns, making it possible to divide a program into smaller, more manageable parts. This makes it easier to understand, test, and modify the program.

  3. Flexibility: Abstraction allows for the implementation of an object to change without affecting the code that uses it. This makes it possible to update or replace the implementation of an object without having to make changes to the code that uses it.

  4. Simplicity: Abstraction allows for the creation of simplified code by hiding the implementation details of an object and only showing the essential characteristics and behaviors of the object. This makes it easier to understand and use the object.

  5. Extensibility: Abstraction allows for the addition of new features and functionality to a program without having to make changes to existing code. This makes it possible to extend the capabilities of a program without having to rewrite existing code.

  6. Polymorphism: Abstraction allows for the use of objects of different classes interchangeably, as long as they implement the same methods or interfaces. This makes it possible to write more flexible and extensible code.

  7. Security: Abstraction allows for the hiding of the internal state of an object, making it more difficult for external code to access or modify the object's state. This helps to maintain the integrity of the object's state and protect it from unauthorized access.

In summary, abstraction allows for code reusability, modularity, flexibility, simplicity, extensibility, polymorphism and security which ultimately leads to better software design and easier maintenance.


More articles on similar topics,

  1. Introduction to Object-Oriented Programming.

  2. Inheritance in Java - A Detailed Explanation.

  3. Polymorphism in Java - A Detailed Explanation.

  4. Python, not a snake, but a programming language!

  5. How to get started with Git and GitHub.

Did you find this article valuable?

Support Abhishek Sharma by becoming a sponsor. Any amount is appreciated!