Abstraction is the process of hiding the complex implementation details and showing only the essential features of an object. It allows you to focus on the what a system does, rather than how it does it. The goal is to reduce complexity and increase efficiency by providing a simpler interface.
Note:
- Abstraction uses only the abstract keyword.
- Without an abstract keyword, you cannot use the abstraction.
Key Points about Abstraction
Hiding Complexity: With abstraction, you focus on what is necessary and hide the unnecessary complexities.
Exposing only Essential Features: In real-world examples, an object might have many properties and methods, but you only expose what is needed and hide others.
Types of Abstraction
We can achieve Abstraction in two ways.
1. Using Abstract Classes
2. Using Interfaces
Java Abstraction – Questions and Answers
Q 1: What is abstraction in Java?
Ans: Abstraction hides implementation details and shows only essential features.
Q 2: Why is abstraction used?
Ans: To reduce complexity and improve code maintainability.
Q 3: How is abstraction achieved in Java?
Ans: Using abstract classes and interfaces.
Q 4: Does abstraction support code reusability?
Ans: Yes, it promotes reusable and modular code.
Q 5: Can abstraction be partial?
Ans: Yes, abstract classes can have both abstract and concrete methods.
Java Abstraction – Objective Questions (MCQs)
Q1. Which of the following best defines abstraction in Java?
Q2. Which keyword is used to declare an abstract class in Java?
Q3. What happens if an abstract class has a method without implementation and the subclass does not override it?
Q4. Can an abstract class have a constructor in Java?
Q5. Which of the following statements about abstract methods is true?