Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the spectra-pro domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home4/mahidhar/public_html/wp-includes/functions.php on line 6114
Java Abstraction | tutorialQ

Java Abstraction

Abstraction in Java

Abstraction is one of the four fundamental principles of object-oriented programming (OOP) along with inheritance, polymorphism, and encapsulation. It involves hiding the complex implementation details and showing only the essential features of an object. The main purpose of abstraction is to handle complexity by concealing unnecessary details from the user and exposing only the relevant aspects of the object.

Key Concepts of Abstraction

  • Hiding Complexity: Abstraction allows you to hide complex implementation details and expose only the necessary parts.
  • Focus on What, Not How: Abstraction focuses on what an object does rather than how it does it.
  • Interface and Abstract Classes: Abstraction in Java is achieved using interfaces and abstract classes.

Implementing Abstraction in Java

Abstract Classes

An abstract class in Java is a class that cannot be instantiated. It is used to define a template for subclasses. An abstract class can have both abstract methods (without a body) and concrete methods (with a body).

Example: Abstract Class

Java
abstract class Vehicle {
    // Abstract method (does not have a body)
    abstract void start();

    // Concrete method
    void stop() {
        System.out.println("Vehicle stopped.");
    }
}

class Car extends Vehicle {
    // Implementation of the abstract method
    @Override
    void start() {
        System.out.println("Car started.");
    }
}

class Bike extends Vehicle {
    // Implementation of the abstract method
    @Override
    void start() {
        System.out.println("Bike started.");
    }
}

public class TestAbstraction {
    public static void main(String[] args) {
        Vehicle myCar = new Car();
        Vehicle myBike = new Bike();

        myCar.start(); // Calls Car's start method
        myCar.stop();  // Calls Vehicle's stop method

        myBike.start(); // Calls Bike's start method
        myBike.stop();  // Calls Vehicle's stop method
    }
}

In this example:

  • The Vehicle class is abstract and contains one abstract method start() and one concrete method stop().
  • The Car and Bike classes extend the Vehicle class and provide implementations for the start() method.

Interfaces

An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields and constructors. All methods in an interface are implicitly abstract unless they are default or static methods.

Example: Interface

Java
interface Animal {
    void sound();
    void eat();
}

class Dog implements Animal {
    @Override
    public void sound() {
        System.out.println("Dog barks.");
    }

    @Override
    public void eat() {
        System.out.println("Dog eats bones.");
    }
}

class Cat implements Animal {
    @Override
    public void sound() {
        System.out.println("Cat meows.");
    }

    @Override
    public void eat() {
        System.out.println("Cat eats fish.");
    }
}

public class TestInterfaceAbstraction {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.sound(); // Calls Dog's sound method
        myDog.eat();   // Calls Dog's eat method

        myCat.sound(); // Calls Cat's sound method
        myCat.eat();   // Calls Cat's eat method
    }
}

In this example:

  • The Animal interface declares two methods: sound() and eat().
  • The Dog and Cat classes implement the Animal interface and provide concrete implementations for the sound() and eat() methods.

Benefits of Abstraction

  • Reduce Complexity: Abstraction helps in reducing the complexity of the code by hiding the unnecessary details.
  • Enhance Maintainability: With abstraction, code is more organized and easier to maintain.
  • Promote Reusability: Abstraction allows for the creation of reusable components.
  • Improve Flexibility: Changes in the implementation details do not affect the code that uses the abstracted components.

Real-World Example: Banking System

Consider a banking system where different types of accounts need to be managed. You can use abstraction to define a general Account class and then create specific account types like SavingsAccount and CurrentAccount.

Example: Abstract Class in Banking

Java
abstract class Account {
    private String accountNumber;
    private double balance;

    public Account(String accountNumber, double balance) {
        this.accountNumber = accountNumber;
        this.balance = balance;
    }

    // Abstract method to be implemented by subclasses
    abstract void calculateInterest();

    public void deposit(double amount) {
        balance += amount;
        System.out.println("Deposited: " + amount);
    }

    public void withdraw(double amount) {
        if (balance >= amount) {
            balance -= amount;
            System.out.println("Withdrawn: " + amount);
        } else {
            System.out.println("Insufficient balance.");
        }
    }

    public void showBalance() {
        System.out.println("Account Number: " + accountNumber + ", Balance: " + balance);
    }
}

class SavingsAccount extends Account {
    private double interestRate;

    public SavingsAccount(String accountNumber, double balance, double interestRate) {
        super(accountNumber, balance);
        this.interestRate = interestRate;
    }

    @Override
    void calculateInterest() {
        double interest = (super.balance * interestRate) / 100;
        super.deposit(interest);
        System.out.println("Interest calculated and deposited: " + interest);
    }
}

class CurrentAccount extends Account {
    public CurrentAccount(String accountNumber, double balance) {
        super(accountNumber, balance);
    }

    @Override
    void calculateInterest() {
        System.out.println("No interest for current account.");
    }
}

public class TestBankingAbstraction {
    public static void main(String[] args) {
        Account savings = new SavingsAccount("S12345", 1000, 5);
        Account current = new CurrentAccount("C12345", 2000);

        savings.showBalance();
        savings.calculateInterest();
        savings.showBalance();

        current.showBalance();
        current.calculateInterest();
        current.showBalance();
    }
}

In this example:

  • The Account abstract class defines common properties and methods for different types of accounts.
  • The SavingsAccount and CurrentAccount classes extend the Account class and provide specific implementations for the calculateInterest() method.

Summary

Abstraction is a powerful OOP concept that helps in managing complexity by hiding unnecessary details and exposing only the essential features of an object. By using abstract classes and interfaces, Java provides a way to achieve abstraction, making the code more modular, maintainable, and flexible. Understanding and implementing abstraction allows you to create robust and scalable software systems.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top