Java Inheritance — தமிழ்

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

Java Inheritance

Reuse a relationship, not just some code.

Step-by-step explanationsRunnable JavaPractice + answer

முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.

இந்தப் பாடத்தில்…

  • Explain the key idea behind Inheritance.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

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

Inheritance lets a subclass extend a superclass. Use it for a meaningful is-a relationship and understand what behavior can be overridden. Composition is often clearer when one object simply uses another rather than being a specialized version of it.

02 / Step by step

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

Java Inheritance என்னன்னு தெரியுமா?

Inheritance என்பது ஒரு class-ல இருந்து attributes மற்றும் methods-ஐ inherit பண்றது — code reuse பண்றதுக்கு மிகவும் useful.

Two types of classes:

  • Subclass (child) — ஒரு class-ல இருந்து inherit பண்றது. extends keyword use பண்றோம்
  • Superclass (parent) — யாரால் inherit ஆகுதோ அந்த class

Inheritance Example

Vehicle class-ல இருந்து Car class inherit பண்றதுக்கு:

Final class-ஐ இன்னொரு class inherit பண்ண முடியாது — security மற்றும் performance-க்கு use ஆகுது.

Java reference fragmentJAVA
class Animal {
    String name;
    void eat() { System.out.println(name + " eats."); }
}

class Dog extends Animal {
    @Override
    void eat()  { System.out.println(name + " eats dog food."); }
    void bark() { System.out.println(name + " says: Woof!"); }
}

Dog d = new Dog();
d.name = "Rex";
d.eat();   // Rex eats dog food.
d.bark();  // Rex says: Woof!

Java-வில் multiple inheritance (multiple classes-ஐ extend செய்வது) possible-ஆ?

  1. ஆம், extends keyword-உடன்
  2. இல்லை, Java classes-க்கு multiple inheritance support செய்யாது
  3. Only for abstract classes
  4. Only with static methods
Reveal answer / விடை

இல்லை, Java classes-க்கு multiple inheritance support செய்யாது

Java classes-க்கு multiple inheritance இல்லை — diamond problem avoid செய்ய. ஆனால் multiple interfaces implement செய்யலாம்! `class Dog extends Animal implements Runnable, Serializable`

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 {
        Dog dog = new Dog();
        dog.eat();
        dog.bark();
    }
}
class Animal { void eat() { System.out.println("Eating"); } }
class Dog extends Animal { void bark() { System.out.println("Woof"); } }
Predict first. Then reveal the expected output / வெளியீடு
Eating
Woof

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 a sleep method to Animal and call it using the dog reference.

Reveal the answer and reasoning / விடை

The subclass can inherit the accessible method, so dog.sleep() can invoke that superclass behavior.

தவிர்க்க வேண்டிய தவறு: Constructors are not inherited. A class can extend one direct superclass, though it can implement multiple interfaces.

பாடத்தின் சுருக்கம்

  • 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.