×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Abstraction in Java – Easy Tutorial for Beginners



Last Updated on: 30th Dec 2025 21:02:54 PM

Welcome to this super beginner-friendly tutorial on Abstraction in Java!
If you're just starting with Java or Object-Oriented Programming (OOP), this guide will explain abstraction in the simplest way possible — with real-life examples, diagrams, and easy code.

By the end, you’ll understand:

  • What is Abstraction?

  • Why do we need it?

  • How to achieve Abstraction in Java using Abstract Classes and Interfaces

  • Real-world examples you’ll actually remember!

Let’s begin!

 

What is Abstraction in Simple Words?

Abstraction means hiding complex details and showing only the necessary features of an object.

 

👉 It focuses on what an object does, not how it does it.

 

Real-Life Example of Abstraction

When you drive a car:

  • You press the accelerator → car speeds up
    But do you know how the engine, pistons, fuel system work internally?
    ❌ No

You just use the features (accelerate, brake, steer) without knowing the internal implementation.
✔ This is abstraction!

 

Think of your phone:

  • You press the "Call button → It calls your friend.

  • You don’t know (or care) how the signal travels, how the network works, or how the speaker produces sound.

That’s abstraction!
You only see what it does — not how it does it.

 

In Programming:

Abstraction = Hide the complexity, Show only the essentials.

 

Why Use Abstraction?

Imagine you’re making a game with different animals:

Dog → barks
Cat → meows
Cow → moos
 

All animals make sound, but each makes a different sound.

Instead of writing separate code for each, we can say:

 

"Every animal must have a sound() method" → But let each animal decide how to make its sound."

This is abstraction!

 

How Abstraction is Achieved in Java?

Java provides two ways to achieve abstraction:

  1. Abstract Class (0–100% abstraction)

  2. Interface (100% abstraction)

 

1. Abstraction Using Abstract Class

An abstract class is a class that cannot be instantiated (you can't create objects of it).It’s used to define common behavior for subclasses.

 

An abstract class:

  • May contain abstract methods (without body)

  • May contain non-abstract methods (with body)

  • Cannot be instantiated (can't create object)

 

Example: Abstract Class

abstract class Car {
    // Abstract method (no body)
    abstract void start();

    // Non-abstract method
    void stop() {
        System.out.println("Car is stopping...");
    }
}

// Child class
class Honda extends Car {
    @Override
    void start() {
        System.out.println("Honda starts with key.");
    }
}

public class Main {
    public static void main(String[] args) {
        Car c = new Honda();
        c.start();
        c.stop();
    }
}

 

Output

Honda starts with key.
Car is stopping...

 

Explanation

  • Car is an abstract class → cannot make object

  • start() is abstract → child class must implement it

  • stop() is normal method → directly inherited

  • We create object of Honda and use abstract class reference → abstraction in action

 

2. Abstraction Using Interface

Interface is a fully abstract structure.All methods are abstract (until Java 7).An interface is like a contract. If a class implements an interface, it must implement all its methods.

 

From Java 8 onwards, interfaces can also contain:

  • default methods (with body)

  • static methods (with body)

 

Example: Interface

interface Vehicle {
    void start();
    void stop();
}

class Bike implements Vehicle {
    @Override
    public void start() {
        System.out.println("Bike starts with self start.");
    }

    @Override
    public void stop() {
        System.out.println("Bike stops using disc brake.");
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle v = new Bike();
        v.start();
        v.stop();
    }
}

 

Output

Bike starts with self start.
Bike stops using disc brake.

 

Difference Between Abstract Class & Interface

Feature Abstract Class Interface
Methods Can be abstract + normal Mostly abstract
Variables Can be any type final + static by default
Multiple Inheritance ❌ Not possible ✔ Allowed
Constructor ✔ Yes ❌ No
Use Case When classes are related For common behavior

 

Simple Real-Life Example of Abstraction

ATM Machine

What you see:

  • Enter pin

  • Withdraw money

  • Check balance

What is hidden:

  • Internal transaction logic

  • Connection to bank server

  • Security algorithms

Java implements this idea through abstract classes and interfaces.

 

Practice Questions for You

  1. Create an abstract class Bank with abstract method rateOfInterest().
    Make SBI and HDFC extend it and return different rates.

  2. Create an interface Playable with method play().
    Make Guitar and Piano implement it.

 

Why Abstract Class = 0% to 100% Abstraction? 

An abstract class can contain:

✔ Abstract methods (no body → supports abstraction)
✔ Normal methods (with body → not abstract)
✔ Variables
✔ Constructors
✔ Code implementation

 

This means:

  • If it has 0 abstract methods, abstraction = 0%

  • If it has only abstract methods, abstraction = 100%

  • If it has some abstract + some normal methods, abstraction = partial (30%, 50%, etc.)

 

Example: 0% Abstraction

abstract class A {
    void show() {
        System.out.println("Hello");
    }
}

No abstract method → 0% abstraction

(It is abstract only to prevent object creation)

 

Example: 50% Abstraction

abstract class A {
    abstract void start();   // abstract method
    void stop() {            // normal method
        System.out.println("Stop");
    }
}

One abstract + one normal method → partial abstraction

 

Example: 100% Abstraction

abstract class A {
    abstract void start();
    abstract void stop();
}

Only abstract methods → fully abstract (100%)

But Java still calls this an abstract class, not interface.

 

Why Interface = 100% Abstraction?

Before Java 8:
➡ All methods in interface were abstract → 100% abstraction

After Java 8:

 

Interfaces can have:
Default methods (with body)
Static methods (with body)
Private methods (with body)

 

But STILL:

  • Interface cannot store any implementation logic normally

  • Interface cannot have constructors

  • Interface cannot have instance variables

 

Because:

👉 An interface only defines what should be done
👉 But not how it will be done
👉 Implementation is always handled by child classes

 

Simple Example

interface Vehicle {
    void start();  // abstract
}

 

Child class implements the logic:

class Car implements Vehicle {
    public void start() {
        System.out.println("Car starts");
    }
}

 

➡ Interface only provides rules
➡ Class provides implementation
➡ So abstraction = 100%

Final Summary

Feature Abstract Class Interface
Abstraction Level 0% – 100% 100%
Contains abstract methods Yes Yes
Contains normal methods Yes Java 8 (default/static) but still behaves abstract
Object Creation ❌ Not allowed ❌ Not allowed
Use Case When classes are related For common behavior

 

Conclusion

  • Abstraction hides complex internal logic and shows only necessary details.

  • Implemented using abstract classes and interfaces.

  • Helps in building clean, secure, and scalable software.

 

Keep coding and have fun learning OOP!
You’ve got this!yes


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo