Inheritance — Superclasses, Subclasses and Code Reusability
Explain and apply the concept of inheritance in OOP to promote code reusability
In this lesson
- Explaining how inheritance creates a hierarchy of parent (super) and child (sub) classes
- Extending an existing class in Java and Python, including calling the superclass constructor
- Explaining the effect of private, default, protected and public on access from subclasses and other classes
- Explaining how inheritance promotes code reuse
LearnIs a
LearnInheritance in UML
- - reg: String
- # wheels: int
- + Vehicle(reg: String, wheels: int)
- + getReg(): String
- + describe(): String
- - seats: int
- + Bus(reg: String, seats: int)
- + getSeats(): int
- - engineCc: int
- + Motorbike(reg: String, engineCc: int)
- + getEngineCc(): int
BusVehicleBus inherits from (is a) VehicleMotorbikeVehicleMotorbike inherits from (is a) Vehicle
The subclasses list only what they add. The hollow triangle points to the superclass.
CodeExtending a class
S-BU 42 has 6 wheels S-BU 42 seats 70
Bus has no describe or getReg of its own: both are inherited. Its constructor passes the registration and 6 wheels to the Vehicle constructor and then sets its own attribute.
LearnWhy inheritance helps
LabWho can use an inherited member?
Vehicle is in the package transport and declares speed with the modifier you choose. Find out which classes may use it.
AttributeError: 'Car' object has no attribute '_Car__speed' (name mangling: inside Car, __speed means _Car__speed)
| Java | Vehicle itself | Car: subclass, same package | Garage: not a subclass, same package | SportsCar: subclass, other package | Shop: not a subclass, other package |
|---|---|---|---|---|---|
| private | |||||
| default | |||||
| protected | |||||
| public |
Try every combination to complete the Java table (1 of 20). Switch to Python to compare its conventions.
LearnAccess modifiers and inheritance (Java)
| Modifier | The class itself | Same package | Subclass in another package | Any other class |
|---|---|---|---|---|
private | Yes | No | No | No |
| default (no modifier) | Yes | Yes | No | No |
protected | Yes | Yes | Yes | No |
public | Yes | Yes | Yes | Yes |
A subclass inherits private attributes too (every Bus object has its own reg), but its own code cannot use them by name: it must go through the superclass’s public or protected methods. Python has no default level and enforces nothing except name mangling for __name.
CodePrivate and protected in a subclass
S-TR 7 (2 wheels)
CheckIs inheritance the right relationship?
Should the first class inherit from the second?
SavingsAccountfromBankAccountEnginefromCarStudentfromPersonLibraryfromBookElectricCarfromCarWheelfromBicycle
CheckQuick check
In Java, a Bus constructor sets seats but does not call super(…), and Vehicle has only the constructor Vehicle(String reg, int wheels). What happens?
ExamWorked exam answer
A game has Enemy, Dragon and Goblin classes. Every enemy has health and a position and can move and take damage; dragons can also breathe fire and goblins can steal gold. Explain how inheritance would promote code reuse in this game.
ReviewMistakes examiners see
Saying that a subclass “cannot see” private attributes and so does not have them: it has them, but must use the superclass’s methods.
Using inheritance for “has a” relationships, e.g. making Engine a subclass of Car.
Repeating the superclass’s attributes in the subclass instead of calling the superclass constructor.
In code answers, show extends / (Superclass) and the call to super.
Exam practiceExam-style questions
4 questions · 11 marks. Answer each one first, then mark it yourself against the markscheme. Your answers are saved in this browser only.
A school’s staff system has an Employee class with private attributes name and salary, a constructor that sets both, and the public accessors getName() and getSalary().
Construct a Teacher class that inherits from Employee, adds a subject, and has a method timetableName() that returns the name followed by the subject in brackets, e.g. Mr Ito (Physics).
A developer suggests making name protected in Employee so that timetableName() can use the attribute directly. Explain one disadvantage of this compared with using getName().
Explain why subject should be in Teacher and not in Employee.
Later, every member of staff must have a pension contribution calculated as 5 % of their salary. Explain how the class hierarchy keeps this change small.
0 of 4 questions marked. Mark every question to see your total.
Exam-style questions written for Praxis IB in the format of the 2027 specimen papers. They are not IB past-paper questions.