Saturday, December 6, 2025

Python Day17

Python Day17

Remove Duplicate using Inheritance :

Duplicate removal in OOP means moving the common(repeated) code into a centralized place(usually a parent class).So that multiple classes can reuse it without rewriting.

Problem with duplicate code                 Solution using inheritance 

Code repeated again and again             Write one -->reuse every where

Hard to update later                               Update only in parent class 

More bugs                                                Less complexity


How inheritance help ?

Common code(name ,age show method) --> in parent class.

unique code in child classes only.

it increase the readability and reusability 

See below repeated twice:

class Student:
    def __init__(self, username: str, age: int, sfee: str):
        self.username = username
        self.age = age
        self.sfee = sfee
        print(self.username, "Student created.", self.age, self.sfee)

std = Student("john_doe", 20, "Paid")

class Faculty:
    def __init__(self, username: str, age: int, sfee: str):
        self.username = username
        self.age = age
        self.sfee = sfee
        print(self.username, "Faculty created.", self.age, self.sfee)
fac = Faculty("dr_smith", 45, "Unpaid")    


See Below code Parent class Comm

class AdminClass:
    def __init__(self, username: str, age: int, sfee: str):
        self.username = username
        self.age = age
        self.sfee = sfee
        print(self.username, "Admin created.", self.age, self.sfee)

class Student(AdminClass):
    def __init__(self, susername: str, sage: int, sfee: str):
        AdminClass.__init__(self, susername, sage, sfee)
        print(self.username, "Student created.", self.age, self.sfee)

class Faculty(AdminClass):
    def __init__(self, fusername: str, fage: int, sfee: str):
        AdminClass.__init__(self, fusername, fage, sfee)
        print(self.username, "Faculty created.", self.age, self.sfee)

std = Student("john_doe", 20, "Paid")
fac = Faculty("dr_smith", 45, "Unpaid")




See Below code Parent class Comm


See Below code Parent class Comm