In this Python oops program, we will create an inheritance example in Python. Python class called Animal with attributes name and color.
Inheritance example in Python
Steps to solve the program
- The Animal class has a constructor method __init__ that takes two parameters: name and color. It initializes two instance variables: self.name to store the name of the animal and self.color to store the color of the animal.
- The class also has a print_details method that prints the name and color of the animal.
- The code creates an object animal of the Animal class by calling its constructor with the arguments “Lion” and “Golden”.
- The print_details method is called on the animal object to print the details of the animal.
- Thus, we have creates an inheritance example in Python.
class Animal:
def __init__(self, name, color):
self.name = name
self.color = color
def print_details(self):
print("Name:", self.name)
print("Color:", self.color)
# Create an object of the Animal class
animal = Animal("Lion", "Golden")
animal.print_details()
Output:
Name: Lion
Color: Golden
Related Articles
Create a Python class called Dog that inherits from the Animal class.
Python oops program to create a class with the constructor.
Python oops program to create a class with an instance variable.
Python oops program to create a class with Instance methods.
Python oops program to create a class with class variables.
Python oops program to create a class with a static method.