Abstraction In Java With Example

Abstraction in Java simply means hiding certain details from the user and showing only essential information to the user.

  • Here is a real-life example of abstraction: Through the ATM machine, banks only show essential information to people, such as withdrawing money and checking account balances, by hiding the internal implementation or mechanism.
  • Another real-life example of abstraction is a car's dashboard. The dashboard provides essential information to the driver, such as speed, fuel level, engine temperature, and warning indicators, without exposing the internal workings of the car's systems.

In Java, we can achieve abstraction by using abstract classes, abstract methods and interfaces.

  • What is abstract class?

In Java, an abstract class is a class that is declared as abstract using the abstract keyword before the class. An abstract class is like a plan or a design that other classes can use as a model. We cannot create a direct object of the abstract class. Abstract classes can contain both abstract and concrete(non-abstract) methods.

  • What is an abstract method?

An abstract method is a method that is declared without a body, which means it does not contain any code inside it. It is simply a method signature without an implementation.

  • What is Interface in Java?

An interface is a class that contains only abstract methods, which means, like an abstract class, it does not contain any concrete methods. The classes that implement the interface are responsible for providing the implementation for the methods defined in the interface.


Example of Abstraction:

// Abstract class representing the ATM machine
abstract class ATM {
    // Abstract method to withdraw money
    abstract void withdrawMoney(double amount);

    // Abstract method to check account balance
    abstract double checkBalance();
}

// Concrete class implementing the ATM abstraction
class BankATM extends ATM {
    private double accountBalance;

    BankATM(double initialBalance) {
        this.accountBalance = initialBalance;
    }

    // Implementing the withdrawMoney method
    void withdrawMoney(double amount) {
        if (amount &ls;= accountBalance) {
            System.out.println("Withdrawing $" + amount);
            accountBalance -= amount;
        } else {
            System.out.println("Insufficient funds!");
        }
    }

    // Implementing the checkBalance method
    double checkBalance() {
        System.out.println("Current Account Balance: $" + accountBalance);
        return accountBalance;
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        // Create an object of the BankATM class
        ATM atm = new BankATM(2000);

        // Use the ATM interface to withdraw money and check balance
        atm.withdrawMoney(500);
        atm.checkBalance();
    }
}

OUTPUT:

Withdrawing $500.0
Current Account Balance: $1500.0


In this example, we have created one abstract class ATM that defines two abstract methods withdrawMoney and checkBalance.  The BankATM class extends the ATM class and provides the implementation for these class methods.

When creating an instance of BankATM and using it through the ATM interface,  we can only access the essential functionality such as withdrawing money and checking the account balance, while the internal implementation details are hidden.

In the above example, you can also use an interface instead of an abstract class. It depends on your requirement. However, remember that an interface cannot contain any concrete methods. Therefore, it is necessary to provide implementations for all the abstract methods present in the interface when inheriting from it in a subclass.

Here is the syntax of an interface:
//interface
public interface InterfaceName {
    // Constant declarations
    // Method signatures (abstract methods)
}

//implementation class
public class ClassName implements InterfaceName {
    // Class body
    // Implement interface methods
}


The keyword interface is used to declare an interface and the implements keyword is used to indicate that the class is implementing the interface.


    Related Topics:

No comments:

Post a Comment

If you have any doubts, please discuss here...👇