Java Inheritance
Reuse a relationship, not just some code.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Inheritance.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
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.
Explore the details
Java Inheritance
Inheritance lets a child class automatically gain all the attributes and methods of a parent class. Write common code once in the parent — all children get it for free.
The extends Keyword
Code anatomy / கோடின் விளக்கம்
| Code | Meaning |
|---|---|
Dog | Child (subclass) — gets everything from Animal |
extends | Keyword that sets up inheritance |
Animal | Parent (superclass) — the class being inherited from |
- Subclass / child — the class that inherits. Uses
extends. - Superclass / parent — the class being inherited from.
- Use
protectedon parent attributes so child classes can access them. - Use
final classto prevent any class from extending it.
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!
// Parent reference holds child object
Animal a = new Dog();
a.name = "Buddy";
a.eat(); // Buddy eats dog food. (runtime type = Dog)A 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 {
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.
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 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.
One mistake to avoid: Constructors are not inherited. A class can extend one direct superclass, though it can implement multiple interfaces.
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.