Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python code to demonstrate inheritance with animal class

class Animal():
    def __init__(self, name, age):
        self.name = name
        self.age = age 

    def speak(self):
        print("I am", self.name, "and I am", self.age, "years old")


class Dog(Animal):
    def __init__(self, name, age)
        self.name = name
        self.age = age
        self.type = "dog"

    # Since we inherit from the animal class we can use the method speak on Dog objects


tim = Dog("Tim", 5)
tim.speak() # This will print "I am Tim and I am 5 years old"
Source by www.techwithtim.net #
 
PREVIOUS NEXT
Tagged: #python #code #demonstrate #inheritance #animal #class
ADD COMMENT
Topic
Name
5+4 =