Inheritance, in object-oriented programming, is the ability of a class to inherit members of another class as part of its own definition. The inheriting class is called a subclass (also “derived class” or  “child class”), and the class inherited from is called the superclass (also “base class” or “parent class”).  

Advantages  

  • Code reusability: Reuse code from existing classes, saving time and promoting efficiency. 
  • Modularity and organization: Organize related classes into a hierarchy, improving code structure and maintainability. 
  • Polymorphism: Treat objects of different subclasses as instances of their parent class, enabling flexibility and code reusability. 
  • Code extensibility: Extend class functionality without modifying the original code, promoting adaptability and scalability. 
  • Method overriding: Customize behavior by providing specialized implementations of inherited methods. 

Superclasses may themselves inherit from other classes, resulting in a hierarchy of classes.  

  • Class A is the superclass, and all the classes in the figure inherit from it. 
  • Subclasses B and E inherit the variable var1 and method method1 from Class A. 
  • Class B defines its own variable var2 and method method2
  • Class E defines its own method method5
  • Class C is a subclass of Class B, so it inherits everything from Class A and Class B. Additionally, it defines its own variable var3 and method method3
  • Class D is also a subclass of Class B, so it inherits everything from Class A and Class B. It defines its own method method4

This hierarchy reduces code duplication and promoting a more organized and maintainable code structure. 

Defining Subclasses in Python 

  • A subtype is something that can be substituted for and behave as its parent type (and its parent type, etc.).  
  • Subclasses serve as subtypes in object-oriented programming.  
  • A subtype is a class that inherits from a parent class (also known as a superclass) and provides specialized behaviour or additional functionality while still maintaining the core characteristics of the parent class. 

The use of subclasses as subtypes allows for code reuse and modularity. In Python, you can implement inheritance using the syntax  

class Subclass(Superclass):,  

where Subclass is the subclass and Superclass is the superclass. The subclass will automatically inherit all the attributes and methods of the superclass, and you can add or modify them as needed. 

  1. Define the Base Class
# Define the base class 
class Vehicle: 
    def __init__(self, brand): 
        self.brand = brand 
    def honk(self): 
        print("Honk!")
  • Base classVehicle:
    • __init__ method initializes the brand attribute.
    • honk method prints "Honk!".

2. Define the Subclass

# Define a subclass 
class Car(Vehicle): 
    def __init__(self, brand, model): 
        # Call the constructor of the base class 
        super().__init__(brand) 
        self.model = model 
    def drive(self): 
        print(f"Driving the {self.brand} {self.model}")
  • SubclassCar:
    • Inherits from Vehicle.
    • __init__ method:
      • Calls the base class constructor using super().__init__(brand) to initialize the brand attribute.
      • Initializes its own model attribute.
    • drive method:
      • Prints a message indicating the brand and model being driven.

3. Create Instances of the Base Class and Subclass

# Create instances of the base class and subclass 
vehicle = Vehicle("Generic") 
car = Car("Toyota", "Camry")
  • Instance of Vehicle called vehicle, with "Generic" as the brand.
  • Instance of Car called car, with "Toyota" as the brand and "Camry" as the model.

4. Use the Methods and Attributes of the Instances

# Use the methods and attributes of the instances
vehicle.honk()  # Output: Honk!
car.honk()      # Output: Honk!
car.drive()     # Output: Driving the Toyota Camry
  • vehicle.honk(): Calls the honk method on the vehicle instance, which prints "Honk!".
  • car.honk(): Calls the honk method on the car instance, which also prints "Honk!".
  • car.drive(): Calls the drive method on the car instance, which prints "Driving the Toyota Camry".

Built-in function type 

In Python, all values are objects, and there is a class definition for each built-in type. To determine the type (class name) of a value, you can use the type() function.

  • The resulting expression, class classname, gives the associated class name for any value.

Built-in function help 

A detailed description of a built-in class can be displayed by using the help function.

  • For programmer-defined classes, such as the Fraction class developed earlier, we use the name of the class (with any required arguments) for creating objects of that type.

For built-in types like strings (str), you can create object instances using quotes around the desired characters of the string:

To create a new list, you surround the list elements with square brackets:

Similar approaches can be used for creating tuples, dictionaries, and sets. Knowing the class names of the built-in types, you can create object instances of each as follows:

When using the class name of a built-in type to create a new object instance, you can use arguments of various types to initialize its value. For example:

An integer can be created from a provided string or float value:

my_int_from_str = int("123")
my_int_from_float = int(123.45)

A list can be created from a provided tuple or string:

my_list_from_tuple = list((1, 2, 3))
my_list_from_str = list("abc")

The built-in class names in Python consist of only lowercase letters. However, programmer-defined classes like the Fraction class are conventionally named with an initial uppercase letter