Java Polymorphism
One contract. Different behavior.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Polymorphism.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
An overridden instance method is selected using the runtime object, even when a variable has a superclass or interface type. This lets calling code use a common contract while individual implementations provide their own behavior.
விரிவான விளக்கங்கள்
Polymorphism: ஒரு interface, பல வடிவங்கள்
Polymorphism என்பது "many forms" — ஒரே method-ஐ different classes differently use பண்றது.
Inherited methods-ஐ override பண்றதுக்கு use ஆகுது. Animal class-ல animalSound() இருக்கு — Dog மற்றும் Cat different sounds குடுக்கும்:
Polymorphism code-ஐ flexible மற்றும் extensible-ஆ வையுங்கள்க்கும். Inheritance-உடன் சேர்ந்து பயன்படுத்தலாம்ம்போது மிகவும் powerful.
Polymorphism in Action — List of Animals
ArrayList<Animal> animals = new ArrayList<>();
animals.add(new Dog());
animals.add(new Cat());
for (Animal a : animals) {
a.sound(); // Java calls Dog.sound() or Cat.sound() automatically
}
// Output: Woof! Meow!| Type | When resolved | Mechanism | Example |
|---|---|---|---|
| Compile-time (static) | At compile time | Method overloading | area(r) vs area(w, h) |
| Runtime (dynamic) | At run time | Method overriding (@Override) | a.sound() dispatches to Dog or Cat |
Runtime polymorphism எப்படி achieve செய்வோம்?
- Method overloading மூலம்
- Method overriding மற்றும் parent reference மூலம்
- Static methods மூலம்
- Final methods மூலம்
Reveal answer / விடை
Method overriding மற்றும் parent reference மூலம்
Parent class reference-ல் child object வைத்து, overridden method call செய்தால் child-ன் version run ஆகும் — இதுவே runtime polymorphism. `Animal a = new Dog(); a.sound();` — Dog-ன் sound() run ஆகும்.
முழுமையான உதாரணம்
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 {
Animal[] pets = {new Dog(), new Cat()};
for (Animal pet : pets) pet.sound();
}
}
class Animal { void sound() { System.out.println("Animal"); } }
class Dog extends Animal { @Override void sound() { System.out.println("Woof"); } }
class Cat extends Animal { @Override void sound() { System.out.println("Meow"); } }Predict first. Then reveal the expected output / வெளியீடு
Woof Meow
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 another Dog to the array and predict the output order.
Reveal the answer and reasoning / விடை
The sequence is Woof, Meow, Woof. The loop calls the same method name on three runtime objects.
தவிர்க்க வேண்டிய தவறு: Fields and static methods do not use the same dynamic dispatch rules as overridden instance methods.
பாடத்தின் சுருக்கம்
- 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.