47. Problem to create an inheritance example in Python

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
  1. 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.
  2. The class also has a print_details method that prints the name and color of the animal.
  3. The code creates an object animal of the Animal class by calling its constructor with the arguments “Lion” and “Golden”.
  4. The print_details method is called on the animal object to print the details of the animal.
  5. 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 ShoppingCart with attributes items and total_cost.

Create a Python class called Dog that inherits from the Animal class.

Leave a Comment