Java Intermediate

0% completed

Previous
Next
Introduction to Abstraction in Java

Abstraction in Java is the process of simplifying complex systems by modeling classes appropriate to the problem, and working at the most relevant level of inheritance for a particular aspect of the problem. It involves hiding the internal implementation details and showing only the essential features of an object. Java achieves abstraction through abstract classes and abstract methods, allowing developers to define templates for other classes to follow.

What is Abstraction?

Abstraction is the concept of exposing only the necessary and relevant parts of an object while hiding the implementation details. It focuses on what an object does instead of how it does it.

Abstract Classes and Methods

  • Abstract Class:

    • A class that cannot be instantiated on its own and is meant to be subclassed.
    • Can contain both abstract methods (without implementation) and concrete methods (with implementation).
    • Declared using the abstract keyword.
  • Abstract Method:

    • A method that is declared without an implementation.
    • Must be implemented by subclasses.
    • Also declared using the abstract keyword.

The abstract Keyword

  • Usage:

    • Abstract Classes: Declared using the abstract keyword before the class keyword.

      public abstract class Vehicle { // Class body }
    • Abstract Methods: Declared within an abstract class without a body.

      public abstract void startEngine();
  • Rules:

    • An abstract class can have both abstract and non-abstract methods.
    • If a class contains at least one abstract method, the class itself must be declared abstract.
    • Abstract methods cannot have a body; they end with a semicolon.
    • Subclasses of an abstract class must provide implementations for all abstract methods, unless the subclass is also declared abstract.

Achieving Abstraction in Java

Abstraction in Java is primarily achieved through the use of abstract classes and abstract methods. Here's how to implement abstraction step-by-step:

  1. Define an Abstract Class:

    • Use the abstract keyword to declare a class that cannot be instantiated.
    • Include both abstract and concrete methods as needed.
    public abstract class Animal { // Concrete method public void eat() { System.out.println("This animal eats food."); } // Abstract method public abstract void makeSound(); }
  2. Declare Abstract Methods:

    • Within the abstract class, declare methods without implementations using the abstract keyword.
    public abstract void makeSound();
  3. Create Subclasses:

    • Subclasses of the abstract class must provide implementations for all abstract methods unless they are also declared abstract.
    public class Dog extends Animal { @Override public void makeSound() { System.out.println("Dog barks."); } }
  4. Instantiate Subclasses:

    • Since abstract classes cannot be instantiated, create objects of the concrete subclasses.
    public class Solution { public static void main(String[] args) { Animal myDog = new Dog(); myDog.eat(); // Output: This animal eats food. myDog.makeSound(); // Output: Dog barks. } }

Example: Abstract Class Vehicle and Subclass Car

In this example, we'll create an abstract class Vehicle with an abstract method startEngine. We'll then create a concrete subclass Car that extends Vehicle and provides an implementation for the startEngine method. The Solution class will demonstrate how abstraction works in practice.

Java
Java

. . . .

Explanation:

  1. Vehicle Abstract Class:

    • Abstract Method (startEngine):
      Declared without an implementation, enforcing that subclasses must provide their own implementation.
  2. Car Concrete Subclass:

    • Constructor:
      Calls the superclass (Vehicle) constructor to initialize brand and model, and initializes numberOfDoors.

    • Overridden Method (startEngine):
      Provides a specific implementation for starting the car's engine, utilizing the numberOfDoors variable.

Abstraction is a pivotal OOP concept that enables developers to manage complexity by focusing on high-level operations while hiding low-level details. In Java, abstraction is achieved through abstract classes and abstract methods, which define templates and contracts for subclasses to implement. This approach ensures that subclasses adhere to a specific interface, promoting consistency and reusability across different parts of an application.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next