Topic 2 - Inheritance & Polymorphism Flashcards
Inheritance
A relationship where a new class (subclass) can receive or inherit properties and methods from an existing class (superclass)
Superclass
The parent class that is being inherited from
Subclass
The child class that inherits from the superclass
Reusability in Inheritance
Subclasses can reuse methods defined in the superclass
Extensibility in Inheritance
Subclasses can extend the logic of methods inherited from the superclass
Inheritance Hierarchy
Classes are organized from general to specific, with more specific classes inheriting from more general ones
Implementing Inheritance
To create a subclass, declare it with the syntax: class Subclass(Superclass)
Subclass __init__ method
Must call the superclass __init__ method to initialize inherited attributes
Accessing Superclass methods
Can be done using Superclass.method(self) or super().method()
Method Overriding
Subclasses can override (redefine) methods inherited from the superclass
Polymorphism
Ability of subclasses to have methods with the same names as methods in their superclasses
Polymorphic Method Call
Program can call the correct method based on the object type
isinstance() function
Used to check if an object is an instance of a specific class