Java Abstraction
Describe what must happen, then supply how.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Abstraction.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
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.
விரிவான விளக்கங்கள்
Abstraction: Implementation-ஐ மறைப்போம்
Abstraction என்பது unnecessary details hide பண்ணி essential information மட்டும் show பண்றது.
Abstract class மற்றும் interface use பண்ணி achieve பண்ணலாம்.
Abstract Class
abstract keyword use பண்ணி abstract class create பண்றோம். Abstract class-ஐ instantiate பண்ண முடியாது — subclass-ல inherit பண்ணணும்:
Abstract method-க்கு body இல்லாது — subclass-ல implement பண்ணணும்.
abstract class Shape {
String color;
abstract double area(); // no body
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; }
}
new Circle("red", 5).describe(); // red shape, area = 78.53975Abstract class-ஐ directly instantiate செய்ய முடியுமா?
- ஆம், new keyword-உடன்
- இல்லை — subclass மூலம் மட்டுமே use செய்யலாம்
- Only if it has a constructor
- Only for static methods
Reveal answer / விடை
இல்லை — subclass மூலம் மட்டுமே use செய்யலாம்
Abstract class-ஐ `new AbstractClass()` என்று create செய்ய முடியாது. Concrete subclass மூலம் மட்டுமே instantiate செய்யலாம். Interview-ல் இது மிக அடிக்கடி கேட்கப்படும் question!
முழுமையான உதாரணம்
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.
நீங்களே முயற்சி செய்யுங்கள்
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.
தவிர்க்க வேண்டிய தவறு: A concrete subclass must implement its inherited abstract methods, or it must also be abstract.
பாடத்தின் சுருக்கம்
- 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.