Python OOP Flashcards
How does inheritance work in python?
- Base and Derived Classes: Inheritance in Python involves a base (or parent) class and a derived (or child) class. The derived class inherits attributes and behaviors from the base class, allowing for code reuse and the creation of more specialized classes.
- Method Overriding: Derived classes can override or extend methods from the base class. When a method is called on a derived class instance, Python first looks for the method in the derived class and then in the base class if it’s not found.
- Multiple Inheritance: Python supports multiple inheritance, where a derived class can inherit from more than one base class. The method resolution order (MRO) determines the order in which base classes are considered when accessing methods.
- Super Function: The super() function allows derived classes to call methods from their base classes, enabling cooperative multiple inheritance and ensuring that all base class methods are called in the correct order.
- Polymorphism: Inheritance enables polymorphism, where objects of different classes can be treated as objects of a common base class. This allows for more flexible and generic code that can work with objects of various classes sharing a common interface.
How do you access parent members in the child class?
- Direct Access: Child classes can directly access public members (attributes and methods) of the parent class using the dot notation, as long as those members are not overridden in the child class.
- Super Function: The super() function allows child classes to access parent class members, especially when overriding methods. It’s commonly used in the constructor (__init__) to initialize the parent class.
- Method Resolution Order (MRO): In multiple inheritance, the MRO determines the order in which parent classes are accessed. You can view the MRO using the mro() method or the __mro__ attribute of a class.
- Private and Protected Members: Private members (prefixed with double underscores) are not directly accessible from child classes. Protected members (prefixed with a single underscore) are conventionally meant for internal use but can still be accessed from child classes.
- Explicit Parent Class Name: You can also access parent class members by explicitly using the parent class name, although this approach is less flexible and not recommended for multiple inheritance scenarios.
What is init method in python?
The __init__ method in Python is a special method called a constructor, which is automatically called when an object of a class is created. It is used to initialize the attributes of the object and perform any setup that is required when the object is instantiated. The __init__ method can accept any number of arguments, which can be used to set the initial values of the object’s attributes. It is defined within a class and has the special name __init__ (with double underscores before and after the word “init”).
How will you check if a class is a child of another class?
This is done by using a method called issubclass() provided by python. The method tells us if any class is a child of another class by returning true or false accordingly.
We can check if an object is an instance of a class by making use of isinstance() method