CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Abstraction in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Abstraction in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 4 min read · 0 Comments
Share: in X

Abstraction in Java is a core pillar of object-oriented programming (OOP) that hides implementation details and exposes only essential features. Imagine driving a car: you don’t need to know how the engine works—just press the accelerator. Similarly, abstraction lets developers focus on what an object does, not how it does it. In this guide, you’ll learn how to implement abstraction using abstract classes, interfaces, and best practices.

Table of Contents

  • What is Abstraction in Java?
    • Real-World Example:
  • How to Achieve Abstraction in Java
    • 1. Abstract Classes and Methods
    • 2. Interfaces
  • Abstract Classes vs. Interfaces
  • Why Use Abstraction?
  • Best Practices for Abstraction
  • Common Abstraction Pitfalls (and Fixes)
    • Instantiating an Abstract Class
    • Forgetting to Implement Abstract Methods
    • Misusing Default Methods
  • Conclusion
  • FAQs About Abstraction in Java
    • What’s the difference between abstraction and encapsulation?
    • Can an abstract class have a constructor?
    • Can interfaces have variables?

What is Abstraction in Java?

Abstraction in Java simplifies complex systems by:

  1. Hiding implementation details (e.g., database connections, algorithm logic).
  2. Exposing only relevant operations through methods.

Real-World Example:

A TV remote has buttons like power and volume. We use them without knowing the internal circuitry—this is abstraction.

How to Achieve Abstraction in Java

Java supports abstraction through:

  1. Abstract Classes
  2. Interfaces

1. Abstract Classes and Methods

An abstract class cannot be instantiated. It can include abstract methods (no body) and concrete methods (with implementation).

Syntax:

abstract class Animal {  
    // Abstract method (no implementation)  
    abstract void sound();  

    // Concrete method  
    void sleep() {  
        System.out.println("Zzz");  
    }  
}  

Example:

class Dog extends Animal {  
    void sound() {  
        System.out.println("Woof!"); // Implementation of abstract method  
    }  
}  

public class Main {  
    public static void main(String[] args) {  
        Dog myDog = new Dog();  
        myDog.sound(); // Output: Woof!  
        myDog.sleep(); // Output: Zzz  
    }  
}  

Key Points:

  • Use the abstract keyword to declare classes/methods.
  • Subclasses must implement all abstract methods.

2. Interfaces

An interface is a fully abstract class with only method signatures (no implementation). Java 8+ allows default and static methods.

Syntax:

interface Vehicle {  
    void start(); // Abstract method  
    default void honk() { // Default method (Java 8+)  
        System.out.println("Beep!");  
    }  
}  

Example:

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 kick.");  
    }  
}  

public class Main {  
    public static void main(String[] args) {  
        Car myCar = new Car();  
        myCar.start(); // Output: Car starts with a key.  
        myCar.honk(); // Output: Beep!  
    }  
}  

Key Points:

  • Classes implement interfaces using implements.
  • A class can implement multiple interfaces (unlike inheritance).

Abstract Classes vs. Interfaces

Abstract ClassInterface
Can have abstract + concrete methods.All methods are abstract by default.
Supports single inheritance.Supports multiple inheritance.
Fields can be public, private, etc.Fields are public static final.
Use for shared code across classes.Use for unrelated classes needing common behavior.

Why Use Abstraction?

  1. Reduces Complexity: Hide irrelevant details.
  2. Promotes Reusability: Define templates for subclasses.
  3. Enhances Security: Expose only necessary features.
  4. Supports Modularity: Isolate changes to specific components.

Best Practices for Abstraction

  1. Use Abstract Classes for Partial Abstraction:
    Share common code (e.g., sleep() in the Animal example).
  2. Use Interfaces for Full Abstraction:
    Define contracts for unrelated classes (e.g., Vehicle for Car and Bike).
  3. Follow Naming Conventions:
    Java follows Interface name adjectives (e.g., Runnable, Drawable).
  4. Avoid Over-Abstraction:
    Don’t create unnecessary layers—balance simplicity and functionality.

Common Abstraction Pitfalls (and Fixes)

Instantiating an Abstract Class

Animal myAnimal = new Animal(); // Error!  

Fix: Create a subclass and instantiate it.

    Forgetting to Implement Abstract Methods

    class Cat extends Animal {} // Error: Missing sound()  

    Fix: Add sound() implementation in Cat.

    Misusing Default Methods

    Overuse can lead to interface bloat. 

    Fix: Reserve defaults for backward compatibility.

      Conclusion

      Abstraction in Java is your toolkit for managing complexity and building scalable systems. By mastering abstract classes and interfaces, you’ll write cleaner, more maintainable code.

      FAQs About Abstraction in Java

      What’s the difference between abstraction and encapsulation?

      Abstraction: Hides complexity (focuses on what an object does).
      Encapsulation: Hides data (protects fields via private access and getters/setters).

      Can an abstract class have a constructor?

      Yes! Subclasses use it during instantiation.

      Can interfaces have variables?

      Only public static final (constants).

      Related Posts:

      • Abstract Class in Java: Bridging Code Reusability…
      • Difference between OOP and POP
      • Interface in Java: Mastering Abstraction and…
      • How to Create Instance of a Class in Java: A…
      • What is JVM (Java Virtual Machine)? Architecture,…
      • Encapsulation in Java
      Tags:javalanguage-fundamentals
      Was this article helpful?
      ← Previous ArticleHow to Create Instance of a Class in Java: A Step-by-Step Guide for Developers
      Next Article →Encapsulation in Java

      Recent Posts

      • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
      • How to Use AWS CloudFront Signed URLs in Spring Boot?
      • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
      • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
      • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
      CoderSathi

      Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

      Linkedin

      Quick Links

      • About
      • Contact

      Popular Topics

      • Java
      • Spring Boot
      • AWS
      • DevOps
      • MongoDB
      • Linux
      • Git
      • How to
      © 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap