What is Inheritance?
In Python, inheritance is one of the fundamental concepts of object-oriented programming (OOP). It refers to the ability of a subclass to inherit properties and behaviors (i.e., methods and attributes) from a parent class. Inheritance allows for code reuse and helps to create a hierarchical structure of classes, where subclasses can specialize or extend the functionality of their parent classes.
To inherit a subclass from another class, you can write its name in parentheses ()
after the subclass name (without space):
Code Description
User
and Staff
. The User
class is the parent class, and the Staff
class is the child class inherited from the User
.The
User
class has a class attribute named role
with the string "User".The
Staff
class does not define any additional attributes or methods. It inherits the role
attribute from the User
class.When
print(Staff.role)
is called, it accesses the role
attribute directly from the Staff
class. Since the Staff
class does not have its own role
attribute defined, it inherits the role
attribute from the User
class. Therefore, it will output "User".
This code demonstrates that child classes can access attributes from their parent classes, allowing them to use or override them as needed.
Note
The interpreter searches attributes from the Child to the last Parent in order.
Look at the code example:
Code Description
User
and Staff
. The User
class has a class attribute role
with the string "User", an instance attribute name
initialized through the constructor (__init__
), and a method print_name()
that prints the role and name of the user instance.The
Staff
class is derived from the User
class using the pass
statement, meaning it inherits all the attributes and methods from the base class.An instance of the
Staff
class named bob
is created with the name "Bob" passed as an argument to its constructor. When bob.print_name()
is called, it invokes the print_name()
method inherited from the User
class, which prints "Is a User with the name Bob". Note that the self.role
expression in print_name()
refers to the role
attribute of the instance, which in this case is inherited from the User
class.
The code then accesses the role
attribute of the bob
instance using bob.role
. Since the role
attribute is not defined in the Staff
class or the bob
instance, it falls back to the role
attribute of the User
class, and "User" is printed.
In summary, this code demonstrates how class attributes can be inherited and accessed by instances, as well as how instance attributes are unique to each instance.
Code Description
Staff
class to override the role
attribute inherited from the User
class. In the updated code, the
Staff
class defines its own role
attribute with the value "Staff". This attribute declaration in the Staff
class shadows the role
attribute inherited from the User
class.When
bob.print_name()
is called, it still executes the print_name()
method inherited from the User
class. However, when accessing the self.role
attribute within the print_name()
method, it now refers to the role
attribute defined in the Staff
class. Therefore, it will output "Is a Staff with the name Bob".Similarly, when print(bob.role)
is called, it accesses the role
attribute from the Staff
class, which has overridden the role
attribute from the User
class. As a result, it will output "Staff".
¿Todo estuvo claro?
Contenido del Curso
In-Depth Python OOP
4. Polymorphism and Abstraction
In-Depth Python OOP
What is Inheritance?
In Python, inheritance is one of the fundamental concepts of object-oriented programming (OOP). It refers to the ability of a subclass to inherit properties and behaviors (i.e., methods and attributes) from a parent class. Inheritance allows for code reuse and helps to create a hierarchical structure of classes, where subclasses can specialize or extend the functionality of their parent classes.
To inherit a subclass from another class, you can write its name in parentheses ()
after the subclass name (without space):
Code Description
User
and Staff
. The User
class is the parent class, and the Staff
class is the child class inherited from the User
.The
User
class has a class attribute named role
with the string "User".The
Staff
class does not define any additional attributes or methods. It inherits the role
attribute from the User
class.When
print(Staff.role)
is called, it accesses the role
attribute directly from the Staff
class. Since the Staff
class does not have its own role
attribute defined, it inherits the role
attribute from the User
class. Therefore, it will output "User".
This code demonstrates that child classes can access attributes from their parent classes, allowing them to use or override them as needed.
Note
The interpreter searches attributes from the Child to the last Parent in order.
Look at the code example:
Code Description
User
and Staff
. The User
class has a class attribute role
with the string "User", an instance attribute name
initialized through the constructor (__init__
), and a method print_name()
that prints the role and name of the user instance.The
Staff
class is derived from the User
class using the pass
statement, meaning it inherits all the attributes and methods from the base class.An instance of the
Staff
class named bob
is created with the name "Bob" passed as an argument to its constructor. When bob.print_name()
is called, it invokes the print_name()
method inherited from the User
class, which prints "Is a User with the name Bob". Note that the self.role
expression in print_name()
refers to the role
attribute of the instance, which in this case is inherited from the User
class.
The code then accesses the role
attribute of the bob
instance using bob.role
. Since the role
attribute is not defined in the Staff
class or the bob
instance, it falls back to the role
attribute of the User
class, and "User" is printed.
In summary, this code demonstrates how class attributes can be inherited and accessed by instances, as well as how instance attributes are unique to each instance.
Code Description
Staff
class to override the role
attribute inherited from the User
class. In the updated code, the
Staff
class defines its own role
attribute with the value "Staff". This attribute declaration in the Staff
class shadows the role
attribute inherited from the User
class.When
bob.print_name()
is called, it still executes the print_name()
method inherited from the User
class. However, when accessing the self.role
attribute within the print_name()
method, it now refers to the role
attribute defined in the Staff
class. Therefore, it will output "Is a Staff with the name Bob".Similarly, when print(bob.role)
is called, it accesses the role
attribute from the Staff
class, which has overridden the role
attribute from the User
class. As a result, it will output "Staff".
¿Todo estuvo claro?