DS160123 Using Super() in Inheritance

1st ques:
1 parent class and 1 child class
use constructor and super method for constructor

2nd ques:
1 parent class and 1 child class
name of the methods should be same and use super method and print

class Tree:
    def __init__(self,trunk_color,root_count,branch_count):
        self.trunk = trunk_color
        self.root = root_count
        self.branch = branch_count
    def create(self):
        print('basic tree is created.')

class Oak(Tree):
    def __init__(self,trunk_color,root_count,branch_count,type):
        # Inhereting Constructor Of Parent Class
        self.type = type

class Birch(Tree):
    def create(self):
        # Inhereting Method of Parent Class
        print('Tree is a birch tree')

oak = Oak('Brown','Many',10,'Willow')
print(f'Oak trunk color is {oak.trunk}')
print(f'Oak have {oak.trunk} barches')
birch = Birch('White','Some',8)
Oak trunk color is Brown
Oak have Brown barches

basic tree is created.
Tree is a birch tree

Keep it up

1 Like