Python Inheritance The Super – Other Class

CWC
8 Min Read
Python Inheritance The Super – Other Class

Python Inheritance The Super – Other Class – Python is a very popular programming language that is used to create programs. A python is a dynamic object-oriented programming language that is used for creating programs. In the Python programming language, inheritance is a very common concept. If you are learning the Python language, you must have heard about inheritance. So, today I am going to explain to you inheritance how it works.

The super keyword is used to inherit properties from the parent class. If you are familiar with Python programming then you will surely know that super keyword.

Let’s understand what is inheritance in Python

Inheritance is a type of relationship between two classes. The parent class has child classes and the child class inherits all the properties and methods of the parent class.

Python is very simple to understand and easy to learn. When it comes to understanding inheritance in Python, it is not difficult at all.

In the inheritance, the parent class is the superclass and the child class is the subclass. Let us see the example of inheritance.

Python Inheritance The Super – Other Class

Example:

Suppose you want to create a program for a class. So, you have created a class named Person and you have created a function named name() in this class. This function returns the name of the person. Now, you have created two classes which are student and teacher. In the student class, you have created a function named learn() that will return the number of times the student has studied. Now, you want to write a program that will calculate the grade of a student.

So, you have created a method that will calculate the grade of a student. And the method is called get_grade(). This method will accept two arguments, the first one is the number of times the student has studied and the second one is the number of times the teacher has taught the subject.

So, the student has learned the subject a total of 10 times and the teacher has taught the subject a total of 5 times. So, what is the output of this method?

Here is the code for the above example:


class Person:
def name():
print("The name of the person is",name)
return name
def learn(number):
print("The number of times the student has studied is", number)
return number
def get_grade(number_of_times,number_of_teachers):
if number_of_times>0:
return number_of_times*number_of_teachers/100
else:
return 0
person=Person()
person.learn(10)
print(person.get_grade(10,5))

Output:

The name of the person is Ravi
The number of times the student has studied is 10
The number of times the teacher has taught the subject is 5
The grade of the student is 50

In the above example, the student has studied 10 times and the teacher has taught the subject 5 times. So, the output of the method will be 50.

Python Inheritance – Simple Explanation

Here is an example to understand inheritance in Python.


class Animal:
name = ""
def __init__(self, name):
self.name = name
class Dog(Animal):
def bark(self):
print('Woof')
def bark_again(self):
print('Woof Woof')
class Cat(Animal):
def purr(self):
print('Meow Meow')
def purr_again(self):
print('Meow Meow Meow')
dog = Dog("Jack")
cat = Cat("Frodo")
dog.bark()
cat.purr()
dog.purr_again()
print("Animal", Animal.name, "has", Animal.name, "bark and", Animal.name, "purr.")
print("Dog", dog.name, "and", cat.name, "bark and", cat.name, "purr.")
print("Cat", cat.name, "and", dog.name, "purr.")

Output:

Animal None has None bark and None purr.
Dog Jack and Frodo bark and Meow Meow Meow.
Cat Frodo and Jack purr and Meow Meow Meow.

In this example, the child class has inherited the properties of the parent class. In the above example, the child class inherits the properties of the parent class.

Super Classes – What are they, and why are they important?

Python is one of the most widely used object-oriented programming languages. In Python, there are several built-in data structures and algorithms, which help you to create powerful and scalable applications. One of the most powerful and commonly used data structures in Python is called the Superclass.

Let’s discuss what is a superclass and is its importance in python.

What is a superclass?

A superclass is a type of class from which other classes are derived. Let’s take an example if you want to create a class called Employee and you have two subclasses named Manager and Secretary, so your superclass will be a Person class.

How does a superclass work?

As a superclass is a base class, it can be derived from any class. It means a subclass can inherit the attributes, methods, and properties from the superclass.

When a subclass inherits from a superclass, it has access to the attributes and methods that are defined in the superclass.

Consider the following example:

In the above example, you can see that there are two subclasses named Employee and Person. These two subclasses inherit from the superclass, which is named Person. Now, if you try to create a new object of Employee, then it will inherit the attributes, methods, and properties of the superclass. That is why superclasses are very important for creating a powerful and scalable application.

This is how a superclass works. Superclasses are the base classes from which all the classes are derived. In a nutshell, it can be said that superclasses are the base classes from which you can derive your classes. In the above example, we have seen that the Employee class inherits the attributes, methods, and properties from the Person class.

Another example Inheritance – super – other class


class Point:
def 	init 	(self, x, y):
print(' 	init 	of point')
self.x = x
self.y = y
class Circle(Point):
def 	init 	(self, x, y, r):
print(' 	init 	of circle')
super(Circle, self). 	init 	(x, y)
self.r = r
class Ball(Circle):
def 	init 	(self, x, y, r, z):
print(' 	init 	of ball')
super(Zero, self). 	init 	(x, y)
self.z = z
class Zero:
def 	init 	(self, x, y):
print('really?')
pass
b = Ball(2, 3, 10, 7)
print(b)
# 	init 	of circle
# Traceback (most recent call last):
#	File "bad_shapes.py", line 25, in 
#	b = Ball(2, 3, 10, 7)
#	File "bad_shapes.py", line 16, in 	init 	
#	super(Zero, self). 	init 	(x, y)
# TypeError: super(type, obj): obj must be an instance or subtype of type

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version