Lesson 1 of 0
In Progress
Lesson #14: Inheritance
Inheritance is the process by which one class takes on the attributes and methods of another. Newly formed classes are called child classes, and the classes that child classes are derived from are called parent classes.
Create The First Parent Class
Create a class named Dog
, with name
and breed
properties, and a printname
method:
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
def printname(self):
print(self.name)
dog = Dog(“Boxi”, “Boxer”)
dog.printname()
def __init__(self, name, breed):
self.name = name
self.breed = breed
def printname(self):
print(self.name)
dog = Dog(“Boxi”, “Boxer”)
dog.printname()
Create The First Child Class
Create a class named Boxer
, which will inherit the properties and methods from the Dog
class:
class Boxer(Dog):
pass
pass
Note: you can use the
pass
keyword when you do not want to add anything else to the class.
Now, we can use the class Boxer
to create an object and execute the methods.
Example
dog_1 = Boxer(“Gina”, “Boxer”)
dog_1.printname()
dog_1.printname()