Day 16: Abstraction in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5168

    #1

    Day 16: Abstraction in Java

    What is Abstraction?

    Abstraction means:


    Showing only the necessary data and hiding the unwanted or internal details.


    In simple words, abstraction focuses on what an object does, not how it does it.





    Why Do We Need Abstraction?

    • To reduce complexity
    • To hide implementation details
    • To improve security
    • To make code easy to understand and maintain


    Real-life example:

    When we use an ATM, we only know actions like withdraw, check balance, etc.

    We don’t know how the ATM internally processes the transaction — that part is hidden.





    How to Achieve Abstraction in Java?

    Abstraction is achieved using the abstract keyword.


    The abstract keyword can be used at:
    • Class level
    • Method level





    Important Rules of Abstraction

    • An abstract class can have:
      • Abstract methods
      • Non-abstract (normal) methods
    • We cannot create an object for an abstract class
    • An abstract class can have a constructor
    • Abstract methods do not have a body
    • Child class must override all abstract methods





    Abstract Class Example (From Class)

    Abstract Class: ATM





    public abstract class ATM {

    public abstract void withdraw();

    public void welcomeMessage() {
    System.out.println("Welcome");
    }

    ATM() {
    System.out.println("Constructor");
    }

    public static void main(String[] args) {
    // ATM atm = new ATM(); // Not allowed
    }
    }







    Explanation:

    • ATM is an abstract class
    • withdraw() is an abstract method
    • welcomeMessage() is a non-abstract method
    • Constructor is allowed in abstract class
    • Object creation of ATM is not allowed





    Child Class Implementing Abstract Method

    Class: IciciBank





    public class IciciBank extends ATM {

    public static void main(String[] args) {
    IciciBank icici = new IciciBank();
    }

    @Override
    public void withdraw() {
    System.out.println("withdraw");
    }
    }







    Explanation:

    • IciciBank extends ATM
    • It implements the abstract method withdraw()
    • Without implementing withdraw(), this class would also become abstract
    • Object creation is allowed for IciciBank





    Using the Implemented Methods

    Class: User





    public class User extends IciciBank {

    public static void main(String[] args) {

    User user = new User();
    user.welcomeMessage();
    user.withdraw();
    }
    }







    Explanation:

    • User class indirectly inherits from ATM
    • It can access:
      • welcomeMessage() (non-abstract method)
      • withdraw() (implemented method)
    • Output is achieved using abstraction without knowing internal logic





    Flow of Execution

    1. Object of User is created
    2. Abstract class constructor executes first
    3. welcomeMessage() method is called
    4. withdraw() method implementation is executed





    Key Points to Remember

    • Abstraction hides internal implementation
    • Achieved using abstract keyword
    • Abstract class can have constructor
    • Object creation for abstract class is not allowed
    • Child class must override abstract methods
    • Abstraction focuses on what, not how





    Difference Between Abstract and Normal Class

    Object creation ❌ Not allowed ✔ Allowed
    Abstract methods ✔ Allowed ❌ Not allowed
    Constructor ✔ Allowed ✔ Allowed
    Inheritance ✔ Required Optional





    When to Use Abstraction?

    Use abstraction when:
    • You want to hide implementation details
    • Multiple classes follow a common structure
    • You want to enforce method implementation
    • You want loose coupling in your application







    More...
Working...