Java Inheritance Quiz: Overriding and Polymorphism Practice
Test your Java inheritance skills with 14 questions and two programs on super, overriding, dynamic dispatch, casting, equals and hashCode, and abstract classes.
- Course: Java study plan
- Module: OOP II — inheritance & polymorphism
- Kind: Checkpoint — cleared at 70%
- Reading time: 24 min
- Runtime: OpenJDK 18
Checkpoint — Inheritance & polymorphism is the checkpoint that closes the OOP II — inheritance & polymorphism module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers extends and constructor chaining, the rules of overriding and hiding, dynamic dispatch and casting, the Object methods and their contract, abstract classes and the template method, and the composition-versus-inheritance judgement.
How it works. Fourteen questions and two programs; 70% on the questions and both programs accepted clears the module.
Before you start, make sure you can answer:
- What the first statement of every constructor is, and why
class B extends A {}can fail to compile. - The rules for a valid override, and why
equals(Point)is wrong. - What runs for
Account a = new SavingsAccount(); a.monthEnd();, and for a static method called the same way. - What happens on a failed downcast, and what
instanceofsays aboutnull. - The
equals/hashCodecontract and what breaks if you override only one. - What a template method is, and one reason to prefer composition.
The programs are a small class hierarchy with polymorphic dispatch, and a value class whose equals/hashCode are exercised through a HashSet.
Common questions
Why can class B extends A {} fail to compile in Java?
B gets a default constructor whose first statement is an implicit super(). If A declares only constructors with parameters, such as A(int), that call matches nothing, so B must declare a constructor that calls super(...) with arguments.
Why is equals(Point p) wrong in Java?
It overloads equals instead of overriding Object.equals(Object), so HashSet, List.contains and Objects.equals never call it and fall back to identity. The parameter must be Object; @Override turns this mistake into a compile error.
What runs for Account a = new SavingsAccount(); a.monthEnd();?
SavingsAccount's override of monthEnd, because instance methods dispatch on the object's runtime class. A static method called the same way would run Account's version, since static methods are hidden, not overridden, and resolved by the reference's declared type.
Exercises
Accounts at month end
Write abstract class Account with an owner, a balance in cents, deposit(long), balance(), and an abstract monthEnd(). Savings adds interest at month end: balance += balance * rateBasisPoints / 10000 (integer arithmetic, rate given in basis points). Checking charges a fee: balance -= fee (may go negative). Print with toString() as <owner> <kind> <balance> where kind is savings or checking.
Read an integer n and n lines savings <owner> <balance> <bp> or checking <owner> <balance> <fee>; store them in an Account[], call monthEnd() on every element polymorphically, then print each.
Example: 2 then savings ada 100000 250, checking bob 5000 700 →
ada savings 102500
bob checking 4300Distinct versions
Write final class Version with int major, minor, patch, a static factory parse(String) for "1.2.3", correct equals/hashCode, and toString giving major.minor.patch. Read an integer n and n version strings. Print distinct=<number of distinct versions> (use a HashSet) and then the distinct versions in ascending order, one per line — implement Comparable<Version> comparing major, then minor, then patch, and sort with Collections.sort.
Example: 4 then 1.2.3 1.10.0 1.2.3 0.9.9 →
distinct=3
0.9.9
1.2.3
1.10.0Payroll hierarchy
Write abstract class Employee (name, abstract double pay(), String kind()) with Salaried(name, annual) paying annual / 12, Hourly(name, rate, hours) paying rate × hours with hours over 160 at 1.5×, and Commissioned(name, base, sales) paying base + 5% of sales. Read n lines, sort the employees by pay descending (ties by name), print <name> (<kind>) <pay to two decimals> per line, then payroll <total>.
Example: 3 then salaried ann 60000, hourly bob 20 170, commissioned cy 1000 50000 →
ann (salaried) 5000.00
bob (hourly) 3500.00
cy (commissioned) 3500.00
payroll 12000.00In this module: OOP II — inheritance & polymorphism
- Extending classes — super, constructors and what is inherited
- Overriding — the rules and the traps
- Polymorphism, casting and instanceof
- The Object class — equals, hashCode and toString
- Abstract classes and the template method
- When not to inherit — composition, Liskov and final
- Checkpoint — Inheritance & polymorphism (this lesson)
← When not to inherit — composition, Liskov and final · Interfaces — contracts without implementation →