Java Tutorial

Inheritance

extends, overridden methods, and 'super' (is-a relationship)

What is Inheritance in Java?

Inheritance allows one class (the subclass or child class) to derive its attributes and methods from another class (the superclass or parent class). This is fundamental for promoting code reuse and establishing hierarchies between related objects.
In Java, we use the keyword extends to indicate that one class is a child of another.
java
public class Animal {
    public void eat() {
        System.out.println("Eating...");
    }
}

// Child class!
class Dog extends Animal {
    public void bark() {
        System.out.println("Barking!");
    }
}

Dog d = new Dog();
d.eat(); // Inherited!
d.bark();

Comparisons: The "is-a" Relationship

Wait! Only use inheritance if a child is a version of the parent. For example:
  • A Dog is a animal. (✅ Good use of inheritance!)
  • A Car is NOT a wheel. (❌ Use "Composition" — A Car has a wheel!)

Overriding Methods

A child class can provide its own "specialized" version of a method that already exists in its parent. This is called Method Overriding. Use the @Override tag to tell the compiler exactly what you're doing.
java
class Cat extends Animal {
    @Override
    public void eat() {
        System.out.println("Cat is eating fish!");
    }
}

The "super" Keyword

The super keyword is used to refer to the parent class. You can use it to:
  1. Run the parent constructor: super() MUST be the first line of your constructor!
  2. Run a parent method: super.eat() runs the original version of the method.
java
public Dog() {
    super(); // Call the parent constructor!
    System.out.println("Dog created!");
}

Summary

TermDescription
extendsKeyword used to link a child to a parent
SuperclassThe parent providing the features
SubclassThe child receiving the features
OverrideCreating a specialized version of a method
superLink to the original parent methods/constructor
ObjectThe "Grand-Parent" of ALL classes in Java!
FinalA final class CANNOT have children!
Best ForCode reuse, building object hierarchies
TreeInheritance builds a logical "Tree" of related code
ConstructorParent constructor runs BEFORE the child one!
AccessChildren cannot see private members of the parent!
ProtectedAccessible to the class AND its children
MultipleJava DOES NOT allow inheriting from more than ONE class!
PolymorphismThe ability to treat children as parents (See below!)
SafeInheritance prevents duplication of common code
ChainCalling u.toString() triggers the Object.toString()
RelationshipA hierarchical "is-a" link
InheritedChildren inherit ALL non-private methods and fields
ShadowingCreating a same-named field in a child (AVOID THIS!)
RefactorMove common code to the Parent to clean up many children
AbstractionUse inheritance to define generic behavior (See Abstract)
LimitToo many layers of inheritance makes code hard to read!