Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Superclass | Inheritance
In-Depth Python OOP

Superclass

In Python, a superclass is a class that is inherited from by other classes. It serves as the parent class or base class, providing attributes and methods that can be inherited and utilized by its subclasses. They provide a way to define common functionality and characteristics that can be shared among multiple related classes.

The super() function in Python is used to access and invoke methods or attributes from a superclass within a subclass. It provides a convenient way to delegate or call the superclass's implementation of a method. By using super(), you can call a method defined in the parent class, even if the method has been overridden in the child class. This allows for code reuse and facilitates the extension or customization of the parent class's behavior.

The super() function is typically used inside a subclass to call the parent class's methods. It takes two arguments: the subclass itself (usually self) and the subclass instance (optional). When called without any arguments, super() returns a temporary object of the superclass. You can then call methods or access attributes on this object using dot (.). This provides a way to selectively override or extend the behavior of the parent class while still benefiting from its existing functionality.

The provided code defines two classes: User and NewUser.

The User class has a constructor method __init__() that takes two parameters, username and password. The NewUser class is a subclass of User and extends it by adding additional attributes: name, surname, and age.

The __init__() method in the NewUser class takes five parameters: username, password, name, surname, and age. It calls the __init__() method of the parent class (User) using super().__init__(username, password) to initialize the username and password attributes inherited from the User class. It then sets the name, surname, and age attributes using the provided values.

Note

You can use the super() function for other methods and attributes. The super() call methods from the Parent class and use Parent attributes. This allows us to extend parent methods.

One more example:

Code Description
The provided code defines two classes: User and Admin. The User class has a class attribute role set to the string "User". The Admin class is a subclass of User and overrides the role attribute with the string "Admin".

The info() method of the Admin class prints the value of self.role, which is "Admin", indicating the role of the admin instance. It then calls super().role to access the role attribute from the parent class (User). This demonstrates the use of super() to access attributes or methods defined in the superclass. In this case, super().role returns the value "User".

Note

The super() function is used by popular frameworks like Django.

Everything was clear?

Section 2. Chapter 5
course content

Course Content

In-Depth Python OOP

Superclass

In Python, a superclass is a class that is inherited from by other classes. It serves as the parent class or base class, providing attributes and methods that can be inherited and utilized by its subclasses. They provide a way to define common functionality and characteristics that can be shared among multiple related classes.

The super() function in Python is used to access and invoke methods or attributes from a superclass within a subclass. It provides a convenient way to delegate or call the superclass's implementation of a method. By using super(), you can call a method defined in the parent class, even if the method has been overridden in the child class. This allows for code reuse and facilitates the extension or customization of the parent class's behavior.

The super() function is typically used inside a subclass to call the parent class's methods. It takes two arguments: the subclass itself (usually self) and the subclass instance (optional). When called without any arguments, super() returns a temporary object of the superclass. You can then call methods or access attributes on this object using dot (.). This provides a way to selectively override or extend the behavior of the parent class while still benefiting from its existing functionality.

The provided code defines two classes: User and NewUser.

The User class has a constructor method __init__() that takes two parameters, username and password. The NewUser class is a subclass of User and extends it by adding additional attributes: name, surname, and age.

The __init__() method in the NewUser class takes five parameters: username, password, name, surname, and age. It calls the __init__() method of the parent class (User) using super().__init__(username, password) to initialize the username and password attributes inherited from the User class. It then sets the name, surname, and age attributes using the provided values.

Note

You can use the super() function for other methods and attributes. The super() call methods from the Parent class and use Parent attributes. This allows us to extend parent methods.

One more example:

Code Description
The provided code defines two classes: User and Admin. The User class has a class attribute role set to the string "User". The Admin class is a subclass of User and overrides the role attribute with the string "Admin".

The info() method of the Admin class prints the value of self.role, which is "Admin", indicating the role of the admin instance. It then calls super().role to access the role attribute from the parent class (User). This demonstrates the use of super() to access attributes or methods defined in the superclass. In this case, super().role returns the value "User".

Note

The super() function is used by popular frameworks like Django.

Everything was clear?

Section 2. Chapter 5
some-alt