Abstraction is one of the four fundamental pillars of Object-Oriented Programming (OOP) in Java. It is the process of hiding implementation details and exposing only the essential functionality to the user.
In simple words:
Abstraction tells “WHAT an object does” rather than “HOW it does it.”
For example:
When you drive a car:
- You use the steering wheel.
- You press the accelerator.
- You apply the brakes.
You do not need to know how the engine, gearbox, or fuel injection system works internally.
This is a real-world example of abstraction. Java Abstraction practice questions with solutions help to understand the concepts.
Why Do We Use Abstraction?
Abstraction helps developers:
- Hide unnecessary implementation details
- Improve code security
- Reduce complexity
- Increase maintainability
- Make software easier to extend
- Build reusable code
How is Abstraction Achieved in Java?
Java provides two ways to achieve abstraction:
- Abstract Class
- Interface (covered in the next chapter)
In this chapter, we’ll focus on Abstract Classes.
What is an Abstract Class?
An abstract class is a class that cannot be instantiated (you cannot create its object directly).
It is declared using the abstract keyword.
Example:
abstract class Animal {
abstract void sound();
}
What is an Abstract Method?
An abstract method is a method that has no body.
Example:
abstract void sound();
Every child class must implement this method.
Rules of Abstract Classes
- Declared using the
abstractkeyword. - Cannot create objects directly.
- Can contain:
- Abstract Methods
- Normal Methods
- Constructors
- Variables
- Child classes must implement all abstract methods.
Real-World Examples of Abstraction
Abstraction is widely used in:
- Banking Systems
- ATM Machines
- Vehicle Management Systems
- Hospital Management Software
- Payment Gateways
- Android Development
- Enterprise Applications
- Spring Boot Projects
Before Learning This Chapter
You should already understand:
- Classes
- Objects
- Constructors
- Inheritance
- Polymorphism
- Method Overriding
In this chapter, you’ll solve practical Java abstraction programs commonly asked in interviews, coding tests, and university examinations.
1. Java Program to Create an Abstract Class
Problem Statement
Write a Java program to create an abstract class and implement it using a child class.
Java Solution
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog Barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}
Sample Output
Dog Barks
Explanation
The Animal class is abstract.
It contains an abstract method:
abstract void sound();
The Dog class provides the implementation of this method.
Since abstract classes cannot be instantiated directly, we create an object of the child class.
Concepts Covered
- Abstract Class
- Abstract Method
- Method Implementation
- Inheritance
2. Java Program to Demonstrate Abstract Methods
Problem Statement
Write a Java program to demonstrate abstract methods.
Java Solution
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing Circle");
}
}
public class Main {
public static void main(String[] args) {
Circle circle = new Circle();
circle.draw();
}
}
Sample Output
Drawing Circle
Explanation
The abstract method draw() has no implementation inside the Shape class.
The child class Circle must override it.
Concepts Covered
- Abstract Method
- Abstract Class
- Method Overriding
3. Java Program to Demonstrate Abstract Class with Normal Methods
Problem Statement
Write a Java program to demonstrate that an abstract class can contain both abstract and normal methods.
Java Solution
abstract class Animal {
abstract void sound();
void sleep() {
System.out.println("Animal is Sleeping");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog Barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
dog.sleep();
}
}
Sample Output
Dog Barks
Animal is Sleeping
Explanation
An abstract class can contain:
- Abstract Methods
- Concrete (Normal) Methods
The child class only needs to implement the abstract methods.
Normal methods are inherited automatically.
Concepts Covered
- Abstract Class
- Concrete Methods
- Inheritance
- Method Overriding
4. Java Program to Demonstrate Abstract Class Constructors
Problem Statement
Write a Java program to demonstrate that an abstract class can have constructors.
Java Solution
abstract class Animal {
Animal() {
System.out.println("Animal Constructor Called");
}
abstract void sound();
}
class Dog extends Animal {
Dog() {
System.out.println("Dog Constructor Called");
}
@Override
void sound() {
System.out.println("Dog Barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}
Sample Output
Animal Constructor Called
Dog Constructor Called
Dog Barks
Explanation
Although an abstract class cannot be instantiated directly, it can contain constructors.
Whenever a child class object is created:
- Parent abstract class constructor executes first.
- Child class constructor executes next.
- The object becomes fully initialized.
This behavior is useful for initializing common properties shared by all child classes.
Concepts Covered
- Abstract Class
- Constructors
- Constructor Chaining
- Inheritance
5. Java Program to Demonstrate Multiple Child Classes Using an Abstract Class
Problem Statement
Write a Java program where multiple child classes inherit from the same abstract class.
Java Solution
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog Barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat Meows");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
dog.sound();
cat.sound();
}
}
Sample Output
Dog Barks
Cat Meows
Explanation
The abstract class Animal defines a common contract using the abstract method sound().
Both child classes (Dog and Cat) provide their own implementation.
This promotes consistency while allowing different behaviors.
Concepts Covered
- Abstract Class
- Multiple Child Classes
- Method Overriding
- Runtime Polymorphism
6. Java Program to Demonstrate an Abstract Class with Variables
Problem Statement
Write a Java program to demonstrate that an abstract class can contain variables.
Java Solution
abstract class Employee {
String company = "VSIT Computer Education";
abstract void work();
}
class Developer extends Employee {
@Override
void work() {
System.out.println("Company : " + company);
System.out.println("Developer is Writing Java Code");
}
}
public class Main {
public static void main(String[] args) {
Developer developer = new Developer();
developer.work();
}
}
Sample Output
Company : VSIT Computer Education
Developer is Writing Java Code
Explanation
An abstract class can contain:
- Variables
- Constructors
- Normal Methods
- Abstract Methods
The child class automatically inherits the variable and can use it like a normal inherited member.
Concepts Covered
- Abstract Class
- Variables
- Inheritance
- Method Implementation
7. Java Program to Create a Vehicle Abstract Class
Problem Statement
Write a Java program to create an abstract Vehicle class and implement it using a Car class.
Java Solution
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car Starts with Push Button");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start();
}
}
Sample Output
Car Starts with Push Button
Explanation
The Vehicle class defines the abstract method start().
The Car class provides the implementation.
This approach hides the implementation details from the user while enforcing a common contract.
Concepts Covered
- Abstract Class
- Vehicle Example
- Method Overriding
- Abstraction
8. Java Program to Create a Bank Abstract Class
Problem Statement
Write a Java program to create a banking system using abstraction.
Java Solution
abstract class Bank {
abstract void interestRate();
}
class SBI extends Bank {
@Override
void interestRate() {
System.out.println("SBI Interest Rate : 6.5%");
}
}
public class Main {
public static void main(String[] args) {
SBI bank = new SBI();
bank.interestRate();
}
}
Sample Output
SBI Interest Rate : 6.5%
Explanation
The abstract class defines a common banking operation.
Different banks can implement their own interest calculation logic.
This is one of the most common real-world uses of abstraction.
Concepts Covered
- Abstract Class
- Banking Example
- Method Implementation
- Real-World Java Program
9. Java Program to Demonstrate Employee Abstract Class
Problem Statement
Write a Java program where an abstract employee class is implemented by different employee types.
Java Solution
abstract class Employee {
abstract void jobRole();
}
class Manager extends Employee {
@Override
void jobRole() {
System.out.println("Manager Handles the Team");
}
}
public class Main {
public static void main(String[] args) {
Manager manager = new Manager();
manager.jobRole();
}
}
Sample Output
Manager Handles the Team
Explanation
The parent abstract class provides the contract.
The child class defines the actual implementation.
This keeps employee-related operations standardized.
Concepts Covered
- Abstract Class
- Employee Example
- Method Overriding
- Inheritance
10. Java Program to Create a Shape Abstract Class
Problem Statement
Write a Java program where different shapes implement the same abstract method.
Java Solution
abstract class Shape {
abstract void area();
}
class Rectangle extends Shape {
@Override
void area() {
System.out.println("Area = Length × Width");
}
}
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.area();
}
}
Sample Output
Area = Length × Width
Explanation
The abstract class defines the method area().
Different shapes such as Rectangle, Circle, and Triangle can provide their own implementations.
This makes the design highly flexible and extensible.
Concepts Covered
- Abstract Class
- Shape Example
- Method Implementation
- Object-Oriented Programming
11. Java Program to Demonstrate an Abstract Class with Multiple Methods
Problem Statement
Write a Java program to demonstrate that an abstract class can contain multiple abstract methods.
Java Solution
abstract class Animal {
abstract void eat();
abstract void sound();
}
class Dog extends Animal {
@Override
void eat() {
System.out.println("Dog Eats Meat");
}
@Override
void sound() {
System.out.println("Dog Barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.sound();
}
}
Sample Output
Dog Eats Meat
Dog Barks
Explanation
An abstract class may contain multiple abstract methods.
Every concrete child class must implement all abstract methods; otherwise, the child class must also be declared abstract.
Concepts Covered
- Abstract Methods
- Multiple Abstract Methods
- Method Overriding
- Inheritance
12. Java Program to Demonstrate Constructor and Abstract Methods Together
Problem Statement
Write a Java program to demonstrate that an abstract class can contain both constructors and abstract methods.
Java Solution
abstract class Vehicle {
Vehicle() {
System.out.println("Vehicle Constructor Executed");
}
abstract void start();
}
class Bike extends Vehicle {
@Override
void start() {
System.out.println("Bike Starts with Self Start");
}
}
public class Main {
public static void main(String[] args) {
Bike bike = new Bike();
bike.start();
}
}
Sample Output
Vehicle Constructor Executed
Bike Starts with Self Start
Explanation
The constructor of the abstract class executes automatically before the child class constructor.
This allows common initialization for all subclasses.
Concepts Covered
- Abstract Class
- Constructors
- Constructor Chaining
- Abstract Methods
13. Java Program to Create a Payment Gateway Using Abstraction
Problem Statement
Write a Java program to create a payment gateway using abstraction.
Java Solution
abstract class Payment {
abstract void pay();
}
class UPI extends Payment {
@Override
void pay() {
System.out.println("Payment Completed using UPI");
}
}
public class Main {
public static void main(String[] args) {
UPI payment = new UPI();
payment.pay();
}
}
Sample Output
Payment Completed using UPI
Explanation
The abstract class defines a common payment operation.
Different payment methods such as UPI, Credit Card, and Net Banking can implement the pay() method differently.
This provides flexibility while keeping a common structure.
Concepts Covered
- Abstraction
- Payment Gateway
- Method Implementation
- Real-World Example
14. Java Program to Create an ATM Machine Example Using Abstraction
Problem Statement
Write a Java program to demonstrate abstraction using an ATM machine example.
Java Solution
abstract class ATM {
abstract void withdraw();
}
class SBIATM extends ATM {
@Override
void withdraw() {
System.out.println("Cash Withdrawn Successfully");
}
}
public class Main {
public static void main(String[] args) {
SBIATM atm = new SBIATM();
atm.withdraw();
}
}
Sample Output
Cash Withdrawn Successfully
Explanation
Users only know they can withdraw money.
The internal process (authentication, balance checking, transaction logging, etc.) remains hidden.
This is a perfect example of abstraction in real life.
Concepts Covered
- Abstraction
- ATM Example
- Hidden Implementation
- Abstract Methods
15. Java Program to Demonstrate a Real-World Hospital Management System Using Abstraction
Problem Statement
Write a Java program to demonstrate abstraction using a hospital management system.
Java Solution
abstract class Doctor {
abstract void treatment();
}
class Cardiologist extends Doctor {
@Override
void treatment() {
System.out.println("Treating Heart Patients");
}
}
public class Main {
public static void main(String[] args) {
Cardiologist doctor = new Cardiologist();
doctor.treatment();
}
}
Sample Output
Treating Heart Patients
Explanation
The abstract class Doctor defines a common operation.
Each specialist doctor implements the treatment process according to their specialization.
This design keeps the system organized and extensible.
Concepts Covered
- Abstraction
- Hospital Management System
- Method Implementation
- Real-World Java Example
Chapter Summary
In this chapter, you learned Java Abstraction, one of the four fundamental pillars of Object-Oriented Programming (OOP). Abstraction focuses on hiding implementation details while exposing only the essential functionality to the user.
Using abstraction, developers can design software that is easier to maintain, extend, and secure. Instead of worrying about how something works internally, users only interact with what the object can do.
During this chapter, you practiced:
- Creating Abstract Classes
- Creating Abstract Methods
- Implementing Abstract Classes
- Using Constructors in Abstract Classes
- Using Variables in Abstract Classes
- Multiple Child Classes
- Vehicle Example
- Banking Example
- Employee Example
- Shape Example
- Payment Gateway Example
- ATM Machine Example
- Hospital Management Example
These examples demonstrate how abstraction is used in real-world Java applications to build flexible and maintainable software.
Key Takeaways
- Abstraction hides implementation details from users.
- Java supports abstraction using:
- Abstract Classes
- Interfaces (covered in the next chapter).
- Abstract classes are declared using the
abstractkeyword. - Abstract classes cannot be instantiated directly.
- Abstract methods do not contain a method body.
- Child classes must implement all abstract methods.
- Abstract classes can contain:
- Variables
- Constructors
- Normal Methods
- Abstract Methods
- Abstraction reduces code complexity.
- It improves software maintainability and scalability.
- Abstraction is widely used in enterprise Java applications.
Frequently Asked Questions (FAQs)
1. What is abstraction in Java?
Abstraction is the process of hiding implementation details while showing only the essential features of an object.
2. How is abstraction achieved in Java?
Java supports abstraction using:
- Abstract Classes
- Interfaces
3. What is an abstract class?
An abstract class is a class declared using the abstract keyword.
It cannot be instantiated directly.
Example:
abstract class Animal {
}
4. What is an abstract method?
An abstract method is a method without a body.
Example:
abstract void sound();
Every concrete child class must implement it.
5. Can an abstract class contain constructors?
Yes.
Although objects of an abstract class cannot be created directly, constructors are executed whenever a child object is created.
6. Can an abstract class contain normal methods?
Yes.
An abstract class can contain:
- Abstract Methods
- Normal Methods
- Constructors
- Variables
7. What is the difference between abstraction and inheritance?
| Abstraction | Inheritance |
|---|---|
| Hides implementation | Reuses existing code |
| Focuses on “What” | Focuses on “Is-A” relationship |
| Uses abstract classes/interfaces | Uses extends keyword |
8. Where is abstraction used in real-world applications?
Abstraction is commonly used in:
- ATM Machines
- Banking Systems
- Hospital Management Software
- Vehicle Management Systems
- Payment Gateways
- Android Applications
- Spring Boot Projects
- Enterprise Java Applications
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
