python inheritance

This commit is contained in:
2025-08-25 22:03:52 +05:30
parent f8c6a08749
commit f40e82a9f0

View File

@ -0,0 +1,31 @@
# Single Inheritance
class Animal:
def __init__(self, name):
self.name = name
def make_sound(self):
print(f"{self.name} makes a sound")
def move_forward(self):
print(f"{self.name} moveing forward.... super class")
class Dog(Animal):
def __init__(self, name, age):
super().__init__(name) # call Animal's __init__
self.age = age
def bark(self):
print(f"{self.name} says Bark!") # name is super class arrtibute
# If child defines the same variable, it overrides the parents version.
def move_forward(self): # method overriding
print(f"{self.name}, ({self.age}) moveing forward.... child class")
dog = Dog("Tommy", 2)
dog.bark()
dog.move_forward()