跳到主要内容

考纲要点

Syllabus 20.1.2 — Inheritance

#PointExam Frequency
1Define inheritance: deriving a new class from an existing class★☆☆
2Understand parent class (base/superclass) and child class (derived/subclass)★★★
3Write class Child(Parent): to declare inheritance★★★
4Call super().__init__(params) in child constructor to initialise inherited attributes★★★
5Override methods: redefine method with exact same name and parameters in child★★★
6Use super().method_name() to call parent method from child★★★
7Understand that child inherits all non-private attributes and methods from parent★★☆
8Child class can add new attributes and new methods not in parent★★★
9Create objects of both parent and child classes in main program★★★
10Demonstrate polymorphism: calling same method on parent and child objects gives different behaviour★★☆

Inheritance Hierarchy Keywords

  • Inheritanceclass Child(Parent):
  • Constructor chainingsuper().__init__(...)
  • Method overriding — redefine in child
  • Code reuse — child reuses parent's code via super()
  • Polymorphism — same method name, different implementations

What You Must Be Able To Do

  1. Read a parent class definition and write a child class that:

    • Calls parent constructor
    • Adds new private attributes
    • Overrides at least one method
  2. Write a main program that:

    • Creates instances of both parent and child
    • Calls methods on both
    • Outputs results
  3. Read data from a text file and create appropriate parent / child objects based on record type

  4. Trace through inheritance code and predict output (paper 1 style)