Java OOP
Abstraction
Complete lesson for Abstraction using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.
Basics — Abstraction
Section: Java OOP. abstract classes and methods: hide how, show what. Compared with interfaces.
What abstraction means in Java
- Show the essential operations, hide the implementation.
abstractclasses and interfaces are the language tools. - An
abstractclass cannot be instantiated. It may have constructors, fields, concrete methods, andabstractmethods (no body). - A concrete subclass must implement every abstract method, or itself be abstract.
abstract class Shape {
final String id;
Shape(String id) {
this.id = id;
}
abstract double area();
void tag() {
System.out.println("shape " + id);
}
}
class Circle extends Shape {
private final double r;
Circle(String id, double r) {
super(id);
this.r = r;
}
@Override
double area() {
return Math.PI * r * r;
}
}
public class AbstractDemo {
public static void main(String[] args) {
Shape s = new Circle("c1", 2);
s.tag();
System.out.println(s.area());
}
} Abstract class vs interface
| Abstract class | Interface (Java 8+) | |
|---|---|---|
| Instantiate | No | No (except via implementing class / lambda) |
| Fields | Any (instance + static) | Constants (public static final) + some private static (Java 9) |
| Constructors | Yes | No |
| Method bodies | Optional (abstract or concrete) | default / static / private |
| Multiple | Extend one class | Implement many interfaces |
| Access | Any modifier on members | Public methods by default (private helpers allowed) |
| Use when | Shared state + partial implementation; IS-A | Capability / role (CAN-DO); mixins of behavior |
Rules of thumb
- Need a constructor or non-constant instance state? Start with an abstract class.
- Need to mix into unrelated classes (
Comparable,Runnable)? Interface. - Template method pattern: abstract class defines the skeleton, subclasses fill in steps.
Advanced — Abstraction
Partial abstraction and sealed types
- A class with zero abstract methods can still be
abstractto blocknew(e.g. utility-ish bases). Rare. - Java 17
sealed abstract class Shape permits Circle, Squaremakes the hierarchy closed — useful with switch exhaustiveness in later Java. - Do not mark everything abstract “to be OOP”. If there is only one implementation and no shared code, a concrete class is enough.
Tricky Interview Points
Q1) Can you new an abstract class?
Answer No. You can have a reference of the abstract type that points to a concrete subclass.
Q2) Can an abstract class have a constructor?
Answer Yes. Subclasses call it via super(...). It still cannot be constructed on its own.
Q3) Abstract class vs interface?
Answer Abstract class: single inheritance, instance state, constructors. Interface: multiple implementation, role-based API, default methods. See the table.
Q4) Must an abstract class have abstract methods?
Answer No. abstract only forbids instantiation. Abstract methods are optional.
Q5) Can an abstract method be private or static or final?
Answer No. private cannot be overridden, static is not overridden, final cannot be overridden. abstract requires a future override.
Q6) What is abstraction vs encapsulation?
Answer Abstraction hides complexity (show a simple API). Encapsulation hides data (protect representation). They work together.
Simple Programs
Template method on an abstract class
abstract class Task {
final void run() {
start();
work();
end();
}
void start() {
System.out.println("start");
}
abstract void work();
void end() {
System.out.println("end");
}
}
public class BackupTask extends Task {
@Override
void work() {
System.out.println("copy files");
}
public static void main(String[] args) {
new BackupTask().run();
}
} Problem Solving Tricks
- Name abstract types with roles (
Repository,Shape), notAbstractManagerHelperImpl. - Keep abstract methods small; put shared validation in concrete methods.
- If two subclasses share a field, it belongs on the abstract parent.
- Prefer interfaces for public library APIs — easier to retrofit onto existing classes.
Practice Programs List
- Create
abstract class Employeewithpay()and two subclassesSalariedandHourly. - Give the abstract class a constructor that stores
name. - Try
new Employee()and record the compiler error. - Add a concrete
describe()on the parent and override it in one child. - Rewrite the same design with an interface and decide which version fits better.