Tools
Tools: Day 16: Abstraction in Java
2026-01-21
0 views
admin
What is Abstraction? ## Why Do We Need Abstraction? ## How to Achieve Abstraction in Java? ## Important Rules of Abstraction ## Abstract Class Example (From Class) ## Abstract Class: ATM ## Explanation: ## Child Class Implementing Abstract Method ## Class: IciciBank ## Explanation: ## Using the Implemented Methods ## Class: User ## Explanation: ## Flow of Execution ## Key Points to Remember ## Difference Between Abstract and Normal Class ## When to Use Abstraction? 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. 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. Abstraction is achieved using the abstract keyword. The abstract keyword can be used at: An abstract class can have: 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 Output is achieved using abstraction without knowing internal logic Use abstraction when: Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse CODE_BLOCK:
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 }
} Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
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 }
} CODE_BLOCK:
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 }
} CODE_BLOCK:
public class IciciBank extends ATM { public static void main(String[] args) { IciciBank icici = new IciciBank(); } @Override public void withdraw() { System.out.println("withdraw"); }
} Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
public class IciciBank extends ATM { public static void main(String[] args) { IciciBank icici = new IciciBank(); } @Override public void withdraw() { System.out.println("withdraw"); }
} CODE_BLOCK:
public class IciciBank extends ATM { public static void main(String[] args) { IciciBank icici = new IciciBank(); } @Override public void withdraw() { System.out.println("withdraw"); }
} CODE_BLOCK:
public class User extends IciciBank { public static void main(String[] args) { User user = new User(); user.welcomeMessage(); user.withdraw(); }
} Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
public class User extends IciciBank { public static void main(String[] args) { User user = new User(); user.welcomeMessage(); user.withdraw(); }
} CODE_BLOCK:
public class User extends IciciBank { public static void main(String[] args) { User user = new User(); user.welcomeMessage(); user.withdraw(); }
} - To reduce complexity
- To hide implementation details
- To improve security
- To make code easy to understand and maintain - Class level
- Method level - An abstract class can have: Abstract methods
Non-abstract (normal) methods
- 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 methods
- Non-abstract (normal) methods - 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 - IciciBank extends ATM
- It implements the abstract method withdraw()
- Without implementing withdraw(), this class would also become abstract
- Object creation is allowed for IciciBank - User class indirectly inherits from ATM
- It can access: welcomeMessage() (non-abstract method) withdraw() (implemented method)
- welcomeMessage() (non-abstract method)
- withdraw() (implemented method)
- Output is achieved using abstraction without knowing internal logic - welcomeMessage() (non-abstract method)
- withdraw() (implemented method) - Object of User is created
- Abstract class constructor executes first
- welcomeMessage() method is called
- withdraw() method implementation is executed - 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 - 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
how-totutorialguidedev.toai