Skip to main content

Abstraction in Java

When I started learning Java, abstraction felt confusing at first. But once I connected it with real-life examples, it started to make sense.

In this blog, I’ll explain abstraction in the simplest way possible—just like how I understood it.


What is Abstraction?

Abstraction means hiding the internal implementation details and showing only the essential functionality.

In my own words:

I focus on what something does, not how it does it.

This idea is actually everywhere in real life.

Real-Life Example (How I Understood It)

Think about a car:

  • I use the steering, brake, and accelerator
  • But I don’t know how the engine works internally

Still, I can drive the car perfectly.

That’s abstraction.

Why Abstraction is Important

When I started building projects, I realized abstraction helps me:

  • Reduce complexity
  • Write cleaner code
  • Hide sensitive logic
  • Make code reusable
  • Easily maintain large applications

How Abstraction is Achieved in Java

In Java, I mainly use two ways:

  • Abstract Classes
  • Interfaces

1. Abstract Class in Java

An abstract class is a class that I cannot create objects from directly. It can have both:

  • Abstract methods (no body)
  • Normal methods (with body)

Example

abstract class Vehicle {
    abstract void start();

    void stop() {
        System.out.println("Vehicle stopped");
    }
}

class Car extends Vehicle {
    void start() {
        System.out.println("Car starts with key");
    }
}

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

My Understanding

  • I define a general idea in Vehicle
  • Then provide actual behavior in Car
  • I can’t create Vehicle directly

2. Interface in Java

An interface is like a blueprint. It only defines methods, and I must implement them in another class.

Example


interface Payment {
    void pay();
}

class UPI implements Payment {
    public void pay() {
        System.out.println("Payment via UPI");
    }
}

class Card implements Payment {
    public void pay() {
        System.out.println("Payment via Card");
    }
}

public class Main {
    public static void main(String[] args) {
        Payment p1 = new UPI();
        p1.pay();

        Payment p2 = new Card();
        p2.pay();
    }
}

My Understanding

  • Same method pay()
  • Different implementations (UPI, Card)
  • Very flexible and reusable

Abstract Class vs Interface (Simple View)

Feature Abstract Class Interface
Methods Can have both abstract and normal methods Mostly contains abstract methods
Implementation Can provide method implementation Only method declaration (implementation in class)
Inheritance A class can extend only one abstract class A class can implement multiple interfaces
Usage Used when classes share common behavior Used for defining a common contract
Object Creation Cannot create object directly Cannot create object directly

Real Use Cases (Where I See This in Projects)

  • Payment systems → Same method, different payment types
  • Banking apps → Deposit/withdraw without knowing backend logic
  • APIs → Only necessary data is exposed
  • Frontend apps → Button click hides backend operations

Abstraction vs Encapsulation

I used to mix these up a lot:

  • Abstraction → Hides implementation
  • Encapsulation → Hides data

👉 Abstraction = Design
👉 Encapsulation = Security

Conclusion

Once I understood abstraction, I stopped worrying about internal complexity and started focusing on clean design.

If you’re a beginner, just remember:

Show only what is needed, hide everything else.

Comments

Popular posts from this blog

Oops Concepts : Inheritance In Java

When I first started learning Java, inheritance felt a bit confusing. But once I understood the basic idea, it became one of the easiest and most powerful concepts in Object-Oriented Programming (OOP). In this blog, I’ll explain inheritance in very simple English, so even if you're a beginner student or aspiring developer, you can understand it easily.  What is Inheritance in Java? In simple words, inheritance means reusing code from another class. I usually think of it like this: A child inherits features from parents Similarly, a class can inherit properties and methods from another class Definition: Inheritance is a mechanism where a child class gets properties and methods from a parent class. Key Terms (Very Important) Parent Class (Superclass) → The class that provides properties Child Class (Subclass) → The class that inherits those properties How Inheritance Works in Java Java uses the keyword:           extends Basic Syntax: This means the Chil...

Understanding Polymorphism in Java (Simple & Beginner-Friendly Guide)

 When I first started learning Java, one concept that really stood out to me was polymorphism . At first, it sounded complex, but once I broke it down, it actually became one of the most powerful and easy-to-understand ideas in Object-Oriented Programming (OOP). What is Polymorphism? Polymorphism simply means “many forms.” In Java, I think of it like this: The same method name can behave differently depending on how I use it. This helps me write cleaner, more flexible, and reusable code. There are two main types of polymorphism in Java: Compile-time Polymorphism (Method Overloading) Run-time Polymorphism (Method Overriding) 1. Compile-time Polymorphism (Method Overloading) What it means When I create multiple methods with the same name but different parameters, it's called method overloading . The difference can be: Number of parameters Type of parameters Syntax Example Here’s a simple example I use to understand it better: My Understanding Even though the method name is the same (...

Resolving the “Script Execution Disabled” Error in PowerShell

  If you’ve encountered the error message: File C:\Program Files\nodejs\node_modules\npm\bin\npm.ps1 cannot be loaded because running scripts is disabled on this system. This guide will help you resolve the issue step-by-step Why Does This Error Occur? By default, PowerShell restricts the execution of scripts for security reasons. This restriction is controlled by the  Execution Policy , which determines which scripts can run on your system. To enable script execution, we need to adjust this policy. Step-by-Step Solution 1. Open PowerShell as Administrator Right-click on the   Start Menu   or press   Win + X . Select   Windows PowerShell (Admin) . This is necessary to make changes to your system’s settings. 2. Check the Current Execution Policy Run the following command to see the current policy for the current user: Get-ExecutionPolicy -Scope CurrentUser You will likely see   Restricted , which is the default policy. 3. Change the Execution Policy To ...