⚡0 XP🔥0

Inheritance and Polymorphism

Lesson 10 of 15 ⏱ 5 min read ✎ 1 exercises ❓ 5-question quiz ▶ Runs Java

Reuse code with extends, call parent constructors and methods with super, override methods, use polymorphism through parent references, final, instanceof and the Object class.

Inheritance lets a class (the subclass or child) reuse and extend another class (the superclass or parent) using extends. It models an is-a relationship: a Manager is an Employee; a SavingsAccount is an Account.

extends and super#

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Hi, I am Kavya, 20 years old.
Kavya is studying B.Tech CSE
  • The child gets all non-private fields and methods of the parent.
  • super(...) calls the parent constructor (first line of the child constructor). If you leave it out, Java inserts super() automatically - which fails if the parent has no no-argument constructor.
  • protected members are accessible in subclasses.
हिंदी में समझें

extends से child class को parent class की चीज़ें मिल जाती हैं। Child के constructor में सबसे पहले super(...) से parent का constructor चलता है, ताकि parent वाला हिस्सा (name, age) पहले तैयार हो जाए। Java में एक class सिर्फ़ एक class को extend कर सकती है।

Method overriding#

A subclass can provide its own version of an inherited method. Mark it with @Override so the compiler checks the signature:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Employee Amit      78000.00
Manager  Sunita   132000.00
Intern   Yash      15000.00
Total payroll: 225000.00

Polymorphism#

That loop is polymorphism: the variable type is Employee, but each object runs its own monthlyPay() and role(). In Java every non-static, non-private, non-final method is virtual, so the version is chosen at run time from the real object (dynamic method dispatch).

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Circle with area 12.57
Rectangle with area 13.50
true
Pattern variable c: Circle
Overloading Overriding
Where same class parent and child
Signature different parameters same parameters
Decided at compile time at run time
Kind of polymorphism static dynamic

final, and what cannot be overridden#

  • A final method cannot be overridden; a final class cannot be extended (String is final).
  • static methods are not overridden (a child static method with the same name only hides the parent's).
  • private methods are not inherited, so they cannot be overridden.
  • An overriding method cannot reduce visibility (a public method cannot become private).

The Object class#

Every class extends java.lang.Object, which provides toString(), equals(), hashCode(), getClass() and more:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Book
true
true

Summary#

  • class Child extends Parent reuses fields and methods; Java allows one parent class.
  • super(...) calls the parent constructor; super.method() calls the parent's version.
  • @Override redefines inherited methods; calls through parent references use the real object's version (polymorphism).
  • final blocks overriding/extension; every class ultimately extends Object.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Vehicle hierarchy

Vehicle has name and double fare(int km) returning km * 10. Create Auto (fare = 25 + 14 per km) and Cab (fare = 50 + 18 per km, minimum 150) that override fare. Loop over an array of Vehicle and print:

Bus: Rs 120.0
Auto: Rs 193.0
Cab: Rs 266.0
Expected output:
Bus: Rs 120.0
Auto: Rs 193.0
Cab: Rs 266.0

Quick Quiz

Q1.

Does Java support multiple inheritance of classes (class C extends A, B)?

Q2.

What does super(name) in a constructor do?

Q3.

Animal a = new Dog(); a.sound(); calls...

Q4.

A final method...

Q5.

What is the root class of every Java class?