Inheritance

Inheritance

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 Classes

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 Classes

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.

class Parent():
    def __init__(self, name, gender, dob, haircolor):
        self.name = name
        self.gender = gender
        self.dob = dob
        self.haircolor = haircolor

class Child(Parent):
    def __init__(self,name, gender, dob, haircolor):
        Parent.__init__(self, name, gender, dob, haircolor)

def main():
  done = False
  while not done:
    print("Menu")
    print("S - Start")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "S":
        print("Start")
        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":
        print("Exiting Game!")
        done = True

main()

Concept

Definition

Class

A class outlines the properties and methods for creating objects. When an object is created from a class, the resulting object is called an instance of a class.

Object

An instance of a class.

Properties

Properties are data that define your object.

Behaviors

Behaviors are operations that define your object

Dot Notation

Dot notation indicates that you’re accessing data or behaviors for a particular object.

Constructor

The constructor method is used to initialize data. It is run as soon as an object of a class is instantiated. Also known as the __init__ method. The __init__ method is usually the first method inside a class definition.

self

When defining an instance method, the first parameter of the method is self. To define a data attribute of the object, you use a variable named self with a dot after it.

Inheritance

Inheritance is when a class uses code constructed within another class.

Parent Class

Parent or base classes create a pattern out of which child or subclasses can be based on.

Child Classes

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.

Last updated