Abstraction in Java
1. Introduction
Abstraction is one of the four fundamental OOP concepts in Java. It is the process of hiding internal implementation details and showing only the essential features of an object.
In simple terms, abstraction answers "What an object does", not "How it does it".
2. Why Use Abstraction in Java?
- To reduce complexity and increase efficiency.
- To hide unnecessary implementation details.
- To increase security by exposing only the required information.
- To promote loose coupling between components.
3. How Abstraction Works in Java?
Java achieves abstraction through:
- Abstract Classes (0 to 100% abstraction)
- Interfaces (100% abstraction prior to Java 8)
Sure! Here's a detailed explanation of Abstract Classes in Java, including syntax, example, key concepts, and differences from interfaces:
4. Abstract Classes in Java

What is an Abstract Class?
An abstract class in Java is a class that cannot be instantiated, meaning you cannot create objects of it directly.
It is designed to be inherited by other classes. An abstract class can contain:
- Abstract methods (methods without a body)
- Concrete methods (methods with a body)
- Constructors
- Instance variables
- Static methods
Why Use Abstract Classes?
Abstract classes are used to provide a base or template for other classes.
They allow defining common behavior while forcing subclasses to implement specific methods.
For example, you may have a general Shape class, but each shape like Circle, Rectangle, etc., must define how to draw itself.
Syntax:
abstract class Shape {
abstract void draw(); // Abstract method - no body
void color() {
System.out.println("Coloring shape"); // Concrete method - with body
}
}
Example:
abstract class Shape {
abstract void draw();
void color() {
System.out.println("Coloring shape");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle(); // Shape reference, Circle object
s.draw(); // Calls Circle's draw()
s.color(); // Calls inherited color() method
}
}
Output:
Drawing Circle
Coloring shape
Explanation:
- Shape is an abstract class.
- It has:
- An abstract method draw(): must be implemented by any subclass.
- A concrete method color(): shared behavior that subclasses inherit directly.
- Circle is a subclass of Shape that provides its own implementation of draw().
- In main(), we use polymorphism to refer to a Circle object using a Shape reference.
Key Points About Abstract Classes:
Feature | Explanation |
---|---|
Instantiation | Cannot create objects of abstract class directly. |
Constructors | Can have constructors (used when subclass objects are created). |
Abstract Methods | Can have one or more abstract methods (no body). |
Concrete Methods | Can also include methods with full implementation. |
Instance Variables | Can have fields like any regular class. |
Static Methods | Can include static methods too. |
Inheritance | Must be extended by a subclass. The subclass must override all abstract methods, or it should also be declared abstract. |
When to Use Abstract Classes:
- When you want to provide common code for all subclasses.
- When you want to force subclasses to implement specific behavior.
- When you need shared fields or methods across related classes.
Sure! Here's a detailed explanation of Interfaces in Java, including syntax, examples, key concepts, and how interfaces evolved in Java 8 and beyond:
5. Interfaces in Java
_1753722189.png)
What is an Interface?
An interface in Java is a blueprint of a class. It defines a contract that other classes must follow.
Interfaces contain abstract methods by default (until Java 7), meaning the method has no body and must be implemented by the class that uses the interface.
From Java 8 onwards, interfaces can also include:
- default methods (with body)
- static methods
From Java 9 onwards, interfaces can also have:
- private methods (used within default or static methods)
Syntax (Java 7 and below):
interface Animal {
void sound(); // implicitly public and abstract
}
Example:
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}
Output:
Dog barks
Explanation:
- Animal is an interface with one method: sound().
- Dog is a class that implements the Animal interface.
- It provides the body for the sound() method.
- You create an object of Dog using an Animal reference (demonstrates polymorphism).
Here's a clear explanation of default and static methods in Java interfaces (introduced in Java 8), with examples and output.
From Java 8 Onwards: Default and Static Methods in Interfaces
Prior to Java 8, interfaces in Java could only contain:
- Abstract methods (without implementation)
- Static final constants
But starting from Java 8, interfaces can also include:
- Default methods: Methods with a body (implementation)
- Static methods: Utility or helper methods that belong to the interface
Default Method
- Declared using the default keyword.
- Provides a default implementation inside the interface.
- The implementing class can use it directly or override it.
Example:
interface Animal {
void sound();
default void sleep() {
System.out.println("Animal sleeps");
}
}
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // Dog's sound method
a.sleep(); // Interface's default method
}
}
Output:
Dog barks
Animal sleeps
Explanation:
- Dog implements sound() but does not override sleep(), so the default method in the interface is used.
Static Method
- Declared with the static keyword.
- Belongs to the interface, not to the implementing class.
- Cannot be overridden.
- Called using the interface name.
Example:
interface Animal {
static void info() {
System.out.println("Animals are living beings.");
}
}
class Dog implements Animal {
// Can't override info()
}
public class Main {
public static void main(String[] args) {
Animal.info(); // Called using interface name
}
}
Output:
Animals are living beings.
Explanation:
- info() is a static method in the interface.
- It can’t be inherited or overridden.
- It's accessed directly using InterfaceName.methodName().
Summary:
Feature | Default Method | Static Method |
---|---|---|
Introduced in | Java 8 | Java 8 |
Usage | Provides default behavior to implementing class | Utility/helper method inside interface |
Can be overridden? | Yes | No |
Called via | Implementing object | Interface name |
Key Features of Interfaces
Feature | Description |
---|---|
Abstract Methods | Declared without implementation (required until Java 7) |
Default Methods | Provide method body (Java 8+) |
Static Methods | Can be called using interface name (Java 8+) |
Private Methods | Helper methods inside interface (Java 9+) |
Multiple Implementation | A class can implement multiple interfaces (supports multiple inheritance) |
Public Modifier | All methods in interfaces are public by default |
No Constructors | Interfaces can't have constructors or instance fields (only constants allowed) |
When to Use an Interface?
- When you want to define a common behavior across unrelated classes.
- When you want to achieve multiple inheritance.
- When you want to create loosely coupled code.
6. Abstract Class vs Interface
Feature | Abstract Class | Interface |
---|---|---|
Method Types | Abstract and Concrete | Abstract, Default, Static |
Access Modifiers | Can be public, protected, etc. | Methods are implicitly public |
Variables | Can have instance variables | Only static final variables |
Inheritance | Single inheritance | Multiple interfaces |
Constructors | Allowed | Not allowed |
7. Real-World Example of Abstraction
Example: ATM Machine
You interact with an ATM to withdraw money. You don't know how it processes your request, connects to your bank, or validates credentials. This is abstraction — hiding the complex logic and showing only relevant features.
Code Example:
abstract class Bank {
abstract int getRateOfInterest();
}
class SBI extends Bank {
int getRateOfInterest() {
return 7;
}
}
class HDFC extends Bank {
int getRateOfInterest() {
return 8;
}
}
public class TestBank {
public static void main(String[] args) {
Bank b = new SBI();
System.out.println("SBI ROI: " + b.getRateOfInterest() + "%");
b = new HDFC();
System.out.println("HDFC ROI: " + b.getRateOfInterest() + "%");
}
}
Output:
SBI ROI: 7%
HDFC ROI: 8%
8. Advantages of Abstraction
- Reduces code duplication.
- Makes code more maintainable and scalable.
- Enhances security and control.
- Helps in achieving loose coupling.
9. Disadvantages of Abstraction
- Increases complexity during initial design.
- Requires extra effort to define interfaces/abstract classes.
- Difficult to debug abstract behavior without understanding the complete flow.
Conclusion
Abstraction is a powerful and essential concept in Java that enables developers to manage complexity by focusing on what an object does rather than how it does it. By using abstract classes and interfaces, Java allows the creation of flexible and scalable systems that are easier to maintain and extend.
Whether you're designing a banking system, a graphics library, or a messaging platform, abstraction helps hide unnecessary internal details and exposes only the relevant functionalities to the user. This leads to cleaner code architecture, better security, and reusability.
Understanding the difference between abstract classes and interfaces, and knowing when to use each, is key to writing effective object-oriented programs in Java.
In summary, abstraction lays the foundation for robust software design by enabling loose coupling, modularity, and clear separation of concerns.
Next Blog- Java Encapsulation Guide