What Abstraction in Java Really Means
In plain terms, abstraction means showing a user only what they actually need and hiding everything underneath. With abstraction in place, users can access just the information that is relevant to them — in Java this is called data abstraction.
A common real-world example: picture a person driving a car. The driver only needs a few essentials — the accelerator, the clutch, and the brakes. There is no need to grasp how the car's internal machinery works. That is abstraction in its clearest form.
Watch the explanation here: Video available in Hindi.
How Java Defines Abstraction
In Java, abstraction means hiding implementation details from the user while exposing only the functionality. The user focuses on what something does, not how it does it. An object holds a lot of information, and abstraction lets us reveal only the relevant part, which reduces programming complexity and effort. It is one of the core pillars of OOPs.
Take a banking application: a customer can check their account balance, view a mini statement, change their password, and use similar features. The software hides the fetch and update operations from the customer, because those add no value for them — the customer simply wants to use the application's functionality.
In Java, abstraction is achieved through abstract classes and interfaces. An abstract class provides anywhere from 1 to 100% abstraction, while an interface can provide full 100% abstraction. Both are covered in more detail below.
How to Achieve Abstraction in Java
1. Abstract Class
Declaring a class with the abstract keyword turns it into an abstract class. Such a class can hold abstract methods (methods without a body) as well as concrete or non-abstract methods (methods with a body). A normal class cannot contain any abstract method. The level of abstraction in an abstract class ranges from 1 to 100%, depending entirely on its abstract methods: a mix of abstract and non-abstract methods places it between 1 and 100, while a class with only abstract methods reaches 100% abstraction. You can include as many abstract methods as your use case requires. For more detail.
2. Interface
An interface is the path to 100% abstraction in Java and serves as a blueprint of a class. Like a class, it can hold methods and variables, but its methods are abstract by default — meaning an interface can contain only methods without bodies. Since Java 7, however, default methods can be created inside an interface. You can read that method here. For more detail.
Abstract Class vs Concrete Class
Abstract Class
- A class declared with the abstract keyword is an abstract class, and it can contain both abstract methods (no body) and concrete/non-abstract methods (with a body).
- It must have at least one abstract method (a method without a body). A normal class cannot have any abstract method.
- It is declared using only the abstract keyword; you cannot declare an abstract class without that special keyword. The keyword tells the JVM to treat it as abstract so it can hold abstract methods.
- It can have abstract methods and concrete (non-abstract) methods. Declaring an abstract method in any class forces that class to be abstract, otherwise a compilation error occurs — this is the general rule of abstract classes.
- An abstract class may or may not contain abstract methods.
- An abstract class cannot be declared final, because final and abstract are opposites in Java. Reason: an abstract class must be inherited by a derived class, since the derived class is responsible for implementing its abstract methods. A final class, on the other hand, cannot be extended (inherited). The two concepts therefore oppose each other.
- You cannot create an object of an abstract class. If Java allowed it, two problems would arise: (i) the abstract method has no body, so calling it through that object would be problematic; (ii) abstract classes are only templates — to create an object the class must be concrete, because an object is concrete. An abstract class is useless without a concrete class, since you can only use it when a concrete class extends it.
Concrete Class
- In Java, a simple class (without the abstract keyword) is considered a concrete class. It implements all abstract methods of its parent abstract class and provides implementations for all methods of the interfaces it implements.
- It is declared simply as a Java class (without the abstract keyword), meaning it is a complete class. Abstract classes are incomplete because they hold abstract methods, whereas concrete classes are considered complete.
- It cannot have any abstract methods. Declaring an abstract method in any class forces that class to be abstract, otherwise a compilation error occurs — this is the general rule of abstract classes.
- It cannot contain any abstract methods.
- A concrete class can be declared final. Any concrete class can be made final as needed, and a final class cannot be extended by any class.
- You can create an object of a concrete class. A concrete class can work without an abstract class, because it is a complete and independent class.
Abstract Class vs Interface
In Java, abstract classes and interfaces are both popular concepts, and both are used to achieve abstraction. Here we look at how they differ.
Declaration and Use
An interface is declared with the interface keyword, and subclasses use the implements keyword to implement interfaces. An abstract class is declared with the abstract keyword, and subclasses use the extends keyword to extend an abstract class.
Variables
An interface has all its variables static and final by default, so you must initialize them or the compiler throws an exception at compile time. An abstract class can have final, non-final, static, and non-static variables; if you declare a final variable, you must initialize it or the compiler throws an exception at compile time.
Method Keywords
An interface has all its methods public and abstract by default. You can declare a method with public, private, abstract, default, static, and strictfp permitted. Using the protected modifier throws a compile-time exception because protected is not permitted. Since Java 8, default and static methods can be defined in an interface; in Java 9, private methods can also be defined. An abstract class is more flexible than an interface when declaring methods — you can define abstract methods with the protected modifier too. In an interface, static and default methods are supported since Java 8, whereas in an abstract class you could define static and default methods even before Java 8.
Inheritance
An interface can extend more than one interface. Interfaces can extend other interfaces, but it can…
Keep Exploring
- Home
- Abstraction in Java
- Static keyword in Java
- Final keyword in Java
- Access modifiers in Java
- Java 8 tutorial
- Default method in interface — section 4
- Default method in interface
- Constructor chaining in Java
Join the Discussion
Abstraction keeps Java code focused on what an object does rather than how it does it. By exposing only the essential behavior through abstract classes and interfaces, you can change the underlying implementation without breaking the code that depends on it. This makes programs easier to read, extend, and maintain over time.
