Java Abstraction
Describe what must happen, then supply how.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Abstraction.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
An abstract class can combine shared behavior with abstract operations that subclasses implement. You cannot directly instantiate the abstract class. The contract allows callers to work with the general concept instead of every implementation detail.
Explore the details
Abstract Classes and Methods
Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with either abstract classes or
interfaces (which you will learn more about in the next chapter).
The abstract keyword is a non-access modifier, used for classes and methods:
- Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
- Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
An abstract class can have both abstract and regular methods:
From the example above, it is not possible to create an object of the Animal class:
To access the abstract class, it must be inherited from another class. Let's convert the Animal class we used in the Polymorphism chapter to an abstract class:
Remember from the Inheritance chapter that we use the extends keyword to inherit from a class.
Why And When To Use Abstract Classes and Methods? To achieve security – hide certain details and only show the important details of an object. Note: Abstraction can also be achieved with Interfaces, which you will learn more about in the next chapter.
abstract class Shape {
String color;
abstract double area(); // no body — subclasses must provide it
void describe() {
System.out.println(color + " shape, area = " + area());
}
}
class Circle extends Shape {
double r;
Circle(String c, double r) { this.color = c; this.r = r; }
double area() { return 3.14159 * r * r; }
}
class Rectangle extends Shape {
double w, h;
Rectangle(String c, double w, double h) { this.color = c; this.w = w; this.h = h; }
double area() { return w * h; }
}
new Circle("red", 5).describe(); // red shape, area = 78.53975
new Rectangle("blue", 4, 6).describe(); // blue shape, area = 24.0A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
Shape shape = new Square(4);
System.out.println(shape.area());
}
}
abstract class Shape { abstract int area(); }
class Square extends Shape {
private final int side;
Square(int side) { this.side = side; }
@Override int area() { return side * side; }
}Predict first. Then reveal the expected output / வெளியீடு
16
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Add a Rectangle subclass whose area multiplies width and height.
Reveal the answer and reasoning / விடை
Implement int area() in Rectangle and use a Shape reference to hold the new Rectangle.
One mistake to avoid: A concrete subclass must implement its inherited abstract methods, or it must also be abstract.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.