Learn Java
import com.learnjava.oopfundamentals.Inheritance;

Inheritance in Java

@Concept

Inheritance lets one class, the subclass, acquire the fields and methods of another, the superclass, using the extends keyword — class Dog extends Animal gives Dog everything Animal already has, plus whatever new fields or methods Dog itself adds. It models a genuine "is-a" relationship: a Dog really is a kind of Animal, so it makes sense for it to automatically have everything an Animal has, without retyping that shared code in every subclass.

Java allows a class to extend only one direct superclass, unlike some languages that permit inheriting from several at once. super refers specifically to the superclass part of an object — super(...) as a constructor's first line calls the superclass's own constructor, and super.methodName() calls the superclass's version of a method the subclass has overridden.

@HowItWorks
1

The extends keyword

class Dog extends Animal makes Dog a subclass that inherits Animal's fields and methods.

2

An "is-a" relationship

Inheritance models genuine kinship, where a Dog really is a specific kind of Animal.

3

Inherited members are automatically available

A subclass doesn't need to redeclare a superclass's fields or methods to use them.

4

Single inheritance of classes

A Java class can extend only one direct superclass, never more than one at a time.

5

super

Refers to the superclass part of an object; super(...) calls the superclass's constructor, and super.method() calls the superclass's version of a method.

@Example
class Animal {
String name;
 
Animal(String name) {
this.name = name;
}
 
void eat() {
System.out.println(name + " is eating");
}
}
 
public class Dog extends Animal {
Dog(String name) {
super(name);
}
 
public static void main(String[] args) {
Dog rex = new Dog("Rex");
rex.eat();
}
}
Output
Rex is eating
1Dog extends Animal, so it inherits Animal's name field and eat() method without redefining either.
2Dog's own constructor calls super(name);, delegating the actual field assignment to Animal's constructor.
3rex.eat() calls the inherited method directly, since Dog never defined its own version.
4That inherited method reads name, which Animal's constructor set through the super(...) call, printing one line.
@UseCases
Sharing common fields and methods across several related classes, like Animal shared by Dog and Cat
Modeling a genuine is-a relationship between two types instead of duplicating shared code
Calling a superclass's constructor to reuse its initialization logic with super(...)
Reaching a superclass's original method implementation from inside an overriding one with super.method()

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.