Inheritance in Java
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.
The extends keyword
class Dog extends Animal makes Dog a subclass that inherits Animal's fields and methods.
An "is-a" relationship
Inheritance models genuine kinship, where a Dog really is a specific kind of Animal.
Inherited members are automatically available
A subclass doesn't need to redeclare a superclass's fields or methods to use them.
Single inheritance of classes
A Java class can extend only one direct superclass, never more than one at a time.
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.
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(); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.