Comment on page
Inheritance is when a class uses code constructed within another class. If we think of inheritance in terms of biology, we can think of a child inheriting certain traits from their parent. That is, a child can inherit a parent’s height or eye color. Children also may share the same last name with their parents.
Classes called child classes or subclasses inherit methods and variables from parent classes or base classes.
To tell Python that a class is a child of another class, add the name of the parent class in parentheses after the name of the new class.
Parent or base classes create a pattern out of which child or subclasses can be based on. Parent classes allow us to create child classes through inheritance without having to write the same code over again each time. Any class can be made into a parent class, so they are each fully functional classes in their own right, rather than just templates.
Child or subclasses are classes that will inherit from the parent class. That means that each child class will be able to make use of the methods and variables of the parent class.
def __init__(self, name, gender, dob, haircolor):
self.name = name
self.gender = gender
self.dob = dob
self.haircolor = haircolor
def __init__(self,name, gender, dob, haircolor):
Parent.__init__(self, name, gender, dob, haircolor)
done = False
while not done:
print("S - Start")
print("Q - Quit")
choice = input("Choice: ")
if choice == "S":
print("Explore My Family Tree")
mom = Parent("Momma", "Female", 1970, "brown")
dad = Parent("Daddy", "Male", 1969, "black")
me = Child("Sam", "Female", 2004, "brown")
elif choice == "Q":
done = True