(( simple overriding, just another init or same name method
next class super(), about use parent init plus it's own,, use parent method and add more to it))
Overriding methods
A child class can also override data members and methods from the parent.
Let's continue with the example in previous lesson, make another child class of the parent class FarmAnimal
, name it Cow
A copy of the FarmAnimal
class:
class FarmAnimal:
def __init__(self, type, legNumber):
self.type = type
self.legNumber = legNumber
def live(self):
print("%s lives on the farm." % (self.type))
def describe(self):
print( "%s has %d legs." %(self.type, self.legNumber) )
# continue with `FarmAnimal` class
class Cow(FarmAnimal):
def __init__(self, color)
self.type = 'Cow'
self.legNumber = 4
self.color = color
def describe(self):
print("%s has %d legs, the color is %s." % (self.type, self.legNumber, self.color))
def makeSound(self):
print("Moo! Moo! Moo!")
# Use the class
cow = Cow(black) # (invoke the __init__() in Cow)
print(cow.legNumber) # 4 (child class constructor sets the fixed value)
cow.describe() # Cow has 4 legs, the color is black.
cow.live() # Cow lives on the farm.
cow2.makeSound() # Moo! Moo! Moo! (method of it's own)
From the above example, you can see, the initialization method in child class Cow
overrides the __init__()
in parent class FarmAnimal
, so when a new instance of Cow
is created, we use Cow(black)
with only one argument black
for color
, no values for type, legNumber
are necessary.
cow
as an instance of Cow
class, has it's initial state values type Cow, legNumber 4
from the child class; it also has one more instance variable color
;
Cow
also overrides the method describe()
, which prints out a message with more information about color than the parent describe()
;
Cow
inherits method live()
from the parent class;
Cow
defines it's own method makeSound()
to print out message of a cow's sound "Moo! Moo! Moo!"
===end here
Pet and Cat classes
See another inheritance example for better understanding: Pet
and Cat
classes.
Pet
class is the parent class, it has 4 fields: name, color, type
, and age
.
Cat
is a child class that derived from Pet
class, inherit the 4 fields from Pet
, but they both have some new feature of their own.
So Cat
and Kitten
are all Pet
s. They all inherit the 4 fields from Pet
, but they both have some new feature of their own.
class Pet:
def __init__ (self, n=' ', c=' ', t=' ', a=0):
self.name = n
self.color = c
self.type = t
self.age = a
def setName (self, n ):
self.name = n
def getName (self):
return self.name
def setColor (self, c ):
self.color = c
def getColor (self):
return self.age
def setType (self, t ):
self.type = t
def getType (self):
return self.type
def setAge (self, a)
self.age = a
def getAge (self):
return "{0} years".format(self.age)
def __str__ (self):
return "{0} is a {1} {2} aged {3} years.".format (self.name, self.color, self.type, self.age)
# Cat is a child class, Pet is the parent class
class Cat( Pet ):
def __init__ (self, n=' ', c=' ', a=0):
self.name = n
self.color = c
self.type = 'cat'
self.age = a
def setType (self, t)
print ('{0} refuses to change type, it will always be a {1} in this life.'. format (self.name, self.type) )
def setAge(self, a):
if 1<= a <30:
self.age = a
else:
print("That's not a fit number for a cat's age.")
Pet
class has four things common to all pets: name, color, type
and age
. The code also has setters and getters (accessors). At the end the __str__( )
method can print a simple message about this pet’s name
, age
, type
and color
.
Cat
is subclass of Pet
.
Cat
class constructor accepts only name, color
and age
, it’s type
is fixed to be 'cat'
. The new constructor overrides the Pet
constructor.
Cat
also overrides the setter setType( )
. If someone tries to change the Cat
type, he will not be able to change it, instead he will get a message “…refuses to change type, it will always be a cat in this life!”
.
'Cat' also overrides setAge()
method, it puts a limit on the age
to be 1<age<30 (in years)
.