subclass in python code example
Example 1: inheritance in python 3 example
class Robot:
def __init__(self, name):
self.name = name
def say_hi(self):
print("Hi, I am " + self.name)
class PhysicianRobot(Robot):
def say_hi(self):
print("Everything will be okay! ")
print(self.name + " takes care of you!")
y = PhysicianRobot("James")
y.say_hi()
Example 2: python subcalss
class SubClass(SuperClass):
super().__init__(self,atr1,atr2)
super().SomeMethod(self)
def OtherMethod(self,atr3):
pass
def OverwrittenMethod(self,atr1,atr4):
pass
Example 3: python subclass
class SuperHero(object):
def getName(self):
raise NotImplementedError
class SuperMan(SuperHero):
def getName(self):
return "Clark Kent"
class SuperManII(SuperHero):
def getName(self):
return "Clark Kent, Jr."
if __name__ == "__main__":
sm = SuperMan()
print sm.getName()
sm2 = SuperManII()
print sm2.getName()