Clean • Professional
Abstraction is one of the four main pillars of Object-Oriented Programming (OOP) — along with Encapsulation, Inheritance, and Polymorphism.
Abstraction means hiding internal implementation details and showing only the essential features of an object.
It allows you to focus on what an object does instead of how it does it.
Think of driving a car:
That’s abstraction — only important details are shown to the user.
In Java, abstraction can be achieved in two main ways:

abstract.Syntax
abstract class ClassName {
abstract void methodName(); // Abstract method (no body)
void normalMethod() { // Concrete method
System.out.println("This is a concrete method");
}
}
Example – Abstract Class
abstract class Animal {
abstract void sound(); // Abstract method
void sleep() {
System.out.println("Animal is sleeping");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class AbstractClassExample {
public static void main(String[] args) {
Animal a = new Dog(); // Reference of abstract class
a.sound();
a.sleep();
}
}
Output:
Dog barks
Animal is sleeping
abstract and does not have a body.Syntax
abstract returnType methodName();
Example – Abstract Method
abstract class Shape {
abstract void draw(); // Abstract method
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a Circle");
}
}
class Rectangle extends Shape {
void draw() {
System.out.println("Drawing a Rectangle");
}
}
public class AbstractMethodExample {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
}
}
Output:
Drawing a Circle
Syntax
interface InterfaceName {
void method1(); // implicitly public and abstract
}
Example – Interface
interface Vehicle {
void start(); // abstract method
}
class Car implements Vehicle {
public void start() {
System.out.println("Car starts with a key");
}
}
class Bike implements Vehicle {
public void start() {
System.out.println("Bike starts with a button");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Vehicle v = new Car();
v.start();
}
}
Output:
Car starts with a key
From Java 8 onwards, interfaces can also have:
Example – Default & Static Methods in Interface
interface Test {
void show(); // abstract method
default void display() {
System.out.println("Default method in interface");
}
static void print() {
System.out.println("Static method in interface");
}
}
class Example implements Test {
public void show() {
System.out.println("Implemented show() method");
}
}
public class InterfaceFeatures {
public static void main(String[] args) {
Example obj = new Example();
obj.show();
obj.display();
Test.print();
}
}
Output:
Implemented show() method
Default method in interface
Static method in interface
[Abstract Class: Vehicle]
/ \\
[Car] (extends) [Bike] (extends)
- start() - start()
- stop()
[Interface: Payment]
-----------------------------
| pay(amount) |
-----------------------------
/ | \\
CreditCardPayment PayPalPayment UPIPayment
Explanation:
stop()) and abstract methods (start()).| Feature | Abstract Class | Interface |
|---|---|---|
| Abstraction Level | Partial (0–100%) | Full (100%) |
| Methods | Abstract + Concrete | Abstract, default, static |
| Access Modifiers | Can be any (public, protected, private) | All methods are public |
| Variables | Instance or static | Always public static final |
| Inheritance | Single inheritance | Multiple inheritance |
| Keyword | extends | implements |
| Use Case | Common base with shared logic | Define contract or capability |
| Use Case | Prefer |
|---|---|
| You want to share common code | Abstract Class |
| You want to define only a contract | Interface |
| You need multiple inheritance | Interface |
| Partial implementation required | Abstract Class |
Scenario: Payment System
We have multiple types of payments (Credit Card, PayPal, UPI), but the user only interacts with a generic Payment interface. The implementation details are hidden — classic abstraction.
// Interface for abstraction
interface Payment {
void pay(double amount); // abstract method
}
// Implementation 1: Credit Card Payment
class CreditCardPayment implements Payment {
public void pay(double amount) {
System.out.println("Paid " + amount + " using Credit Card");
}
}
// Implementation 2: PayPal Payment
class PayPalPayment implements Payment {
public void pay(double amount) {
System.out.println("Paid " + amount + " using PayPal");
}
}
// Implementation 3: UPI Payment
class UPIPayment implements Payment {
public void pay(double amount) {
System.out.println("Paid " + amount + " using UPI");
}
}
// Client code
public class PaymentDemo {
public static void main(String[] args) {
Payment payment;
// User chooses Credit Card
payment = new CreditCardPayment();
payment.pay(1000);
// User switches to PayPal
payment = new PayPalPayment();
payment.pay(500);
}
}
Output:
Paid 1000 using Credit Card
Paid 500 using PayPal| Concept | Description | Example |
|---|---|---|
| Abstract Class | Base class with both abstract and normal methods | abstract class Shape |
| Abstract Method | Declared without a body, implemented in subclass | abstract void draw() |
| Interface | Fully abstract type, supports multiple inheritance | interface Vehicle |
| Default Method | Method with body in interface (Java 8+) | default void show() |