Basic Inheritance

Console Output
>

Basic Inheritance Flow

Click "Run Visualization" in the tools panel to see your inheritance setup in action!

Class Hierarchy

Animal (Parent)
Dog (Child)
Animal (Parent Class)
name, speak()
Dog (Child Class)
breed, speak() (inherited), bark()

Basic Inheritance Example

class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound"

class Dog(Animal): # Basic inheritance
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def bark(self):
return f"{self.name} barks"

Instance Creation

# Create instance
my_dog = Dog("Buddy", "Golden Retriever")

# Test inheritance
print(f"Parent attribute: {my_dog.name}")
print(f"Child attribute: {my_dog.breed}")
print(my_dog.speak())
print(my_dog.bark())

Method Execution

my_dog.speak() called
Inherited from Animal.speak()
Returns: "Buddy makes a sound"
my_dog.bark() called
Returns: "Buddy barks"

Expected Output

Parent attribute: Buddy
Child attribute: Golden Retriever
Animal speak method called
Dog bark method called

Basic Inheritance Benefits

Code Reusability - Dog inherits Animal methods
Single Inheritance - One parent class (Animal)
Simple and Easy to Understand
Method Override - Dog can override speak()