Python Inheritance | class Person|Create a baby category example| Add strategies

Python Inheritance | class Person: | Create a baby category example | Add strategies

Python Inheritance

Inheritance permitsUS to outlinecategory|a category} that inherits all the strategies and properties from another class.

Parent categoryis that thecategory being hereditary from, additionallyknown as base category.

Child categoryis that thecategory that inherits from another categoryadditionallyknown as derived category.

Create a Parent category
Any categoryare often a parent categorythat the syntax is that the same as makingthe other class:

Example
Create a category named Person, with firstname and lastname properties, and a printname method:

class Person:

def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname

def printname(self):
print(self.firstname, self.lastname)

#Use the Person categoryto makeassociate degree object, so execute the printname method:
x = Person(“John”, “Doe”)
x.printname()

Create a babycategory

To create category|a category} that inherits the practicality from another class, send the parent category as a parameter oncemakingthe kid class:

Example
Create a category named Student, which is able to inherit the properties and strategies from the Person class:

class Student(Person):
pass

Note: Use the pass keyword after youdon’twishto featurethe other properties or strategies to the category.

Now the coedcategory has identical properties and strategiesbecause the Person category.

Example
Use the coedcategoryto makeassociate degree object, so execute the printname method:

x = Student(“Mike”, “Olsen”)
x.printname()



Add the __init__() operate
So far we’ve created a babycategory that inherits the properties and strategies from it’s parent.

We want to feature the __init__() operate to the kidcategory (instead of the pass keyword).

Note: The __init__() operateis namedmechanicallywhenthe category is being employedto makea brand newobject.

Example
Add the __init__() operate to the coed class:

class Student(Person):
def __init__(self, fname, lname):
#add properties etc.

When you add the __init__() operatethe kidcategorycannow not inherit the parent’s __init__() operate.

Note: The child’s __init__() operate overrides the inheritance of the parent’s __init__() operate.

To keep the inheritance of the parent’s __init__() operate, add a decision to the parent’s __init__() function:

Example
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)

Now we’vewith successadscititious the __init__() operate, and unbroken the inheritance of the parent categoryand that wesquare measureable to add practicalitywithin the __init__()function.

Use the super() operate

Python even have a super() operatewhich willbuildthe kidcategory inherit all the strategies and properties from it’s parent:

Example
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)

By exploitation the super() operateyou are doing not ought to use the name of the parent partit’llmechanicallyinherit the strategies and properties from it’s parent.

Add Properties
Example
Add a property known as graduationyear to the coed class:

class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
self.graduationyear = 2019

In the example below, the year 2019 ought to be a variable, and passed into the coedcategoryoncemakingstudent objects. To do so, add another parameter within the __init__() function:

Example
Add a year parameter, and pass the proper year oncemaking objects:

class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year

x = Student(“Mike”, “Olsen”, 2019)

Add strategies

Example
Add a techniqueknown as welcome to the coed class:

class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year

def welcome(self):
print(“Welcome”, self.firstname, self.lastname, “to the category of”, self.graduationyear)

Python Inheritance | class Person: | Create a baby category example | Add strategies

Leave a Comment

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