Java Polymorphism — தமிழ்

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 33 / 50 · தமிழ்

Java Polymorphism

One contract. Different behavior.

Step-by-step explanationsRunnable JavaPractice + answer

முக்கிய விளக்கங்கள் தமிழில் உள்ளன. 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.
01 / The concept

அடிப்படையைப் புரிந்துகொள்ளுங்கள்

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.

02 / Step by step

விரிவான விளக்கங்கள்

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

Java reference fragmentJAVA
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!
TypeWhen resolvedMechanismExample
Compile-time (static)At compile timeMethod overloadingarea(r) vs area(w, h)
Runtime (dynamic)At run timeMethod overriding (@Override)a.sound() dispatches to Dog or Cat

Runtime polymorphism எப்படி achieve செய்வோம்?

  1. Method overloading மூலம்
  2. Method overriding மற்றும் parent reference மூலம்
  3. Static methods மூலம்
  4. 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 ஆகும்.

03 / Read and predict

முழுமையான உதாரணம்

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.

Main.java · complete runnable exampleJAVA
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.

04 / Learn by changing

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.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

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.

05 / Your turn

நீங்களே முயற்சி செய்யுங்கள்

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.

Coding Tamilan · Understand the idea. Try the code. Build the habit.