Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Python - Inheritance

Back


Inheritance


Method


Example: Inheritance

print("\n--------Class: Inheritance--------\n")


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

    def __str__(self):
        return f'{self.fname} {self.lname}'

    def printName(self):
        print(f"Name: {self.fname} {self.lname}")


# inherit Person Class


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

    def __str__(self):
        return f"{super().__str__()} - {self.id}"

    def printID(self):
        print(f'ID: {self.id}')


std = Student("John", "Wick", "001")

print(std)                  # John Wick - 001
std.printName()             # Name: John Wick
std.printID()               # ID: 001


TOP