← Java Programming learning pathJava Programming · Module 6 of 10
Inheritance and polymorphism
Reuse and extend classes with extends, override behaviour, and use one interface for many forms.
📖 Key ideas
- class Dog extends Animal: Dog inherits Animal's fields and methods.
- super calls the parent constructor or method.
- @Override replaces a parent method in the child.
- Polymorphism: an Animal variable can hold a Dog; the Dog version of a method runs.
- Java allows single class inheritance only.
💡 Real-life example: A smartphone inherits from phone: it can still call, but it also overrides the screen with touch.
extendsInherits from a class
superRefers to the parent class
@OverrideMarks a method that replaces a parent method
PolymorphismOne reference, many forms