1. 程式人生 > >Python inheritance and polymorphism

Python inheritance and polymorphism

(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

Inheritance allows programmer to create a general class first then later extend it to more specialized class. It also allows programmer to write better code.

Using inheritance you can inherit all access data fields and methods, plus you can add your own methods and fields, thus inheritance provide a way to organize code, rather than rewriting it from scratch.

In object-oriented terminology when class X  extend class Y , then Y  is called super class or base class

and X  is called subclass or derived class. One more point to note that only data fields and method which are not private are accessible by child classes, private data fields and methods are accessible only inside the class.

Syntax to create a subclass is:

123classSubClass(SuperClass):# data fields# instance methods

Let take an example to illustrate the point.

12345678910111213141516171819202122232425262728293031classVehicle:  def__init__(self,name,color):    self.__name=name   # __name is private to Vehicle classself.__color=color  defgetColor(self):    # getColor() function is accessible to class Car    returnself.__color  defsetColor(self,color): # setColor is accessible outside the class    self.__color=color  defgetName(self):# getName() is accessible outside the class    returnself.__nameclassCar(Vehicle):  def__init__(self,name,color,model):# call parent constructor to set name and color      super().__init__(name,color)     self.__model=model  defgetDescription(self):    returnself.getName()+self.__model+" in "+self.getColor()+" color"# in method getDescrition we are able to call getName(), getColor() because they are # accessible to child class through inheritancec=Car("Ford Mustang","red","GT350")print(c.getDescription())print(c.getName())# car has no method getName() but it is accessible through class Vehicle

Expected Output:

12Ford MustangGT350 inred colorFord Mustang

here we have created base class Vehicle  and it’s subclass Car . Notice that we have not defined getName()  in Car  class but we are still able to access it, because class Car  inherits it from Vehicle  class. In the above code super()  method is used to call method of the base class. Here is the how super()  works

Suppose you need to call a method called get_information()  in the base class from child class , you can do so using the following code.

1super().get_information()

similarly you can call base class constructor from child class constructor using the following code.

1super().__init__()

Multiple inheritance

Unlike languages like Java and C#, python allows multiple inheritance i.e you can inherit from multiple classes at the same time like this,

123classSubclass(SuperClass1,SuperClass2,...):  # initializer # methods

Let’s take an example:

123456789101112131415161718classMySuperClass1():  defmethod_super1(self):    print("method_super1 method called")classMySuperClass2():  defmethod_super2(self):    print("method_super2 method called")classChildClass(MySuperClass1,MySuperClass2):  defchild_method(self):    print("child method")c=ChildClass()c.method_super1()c.method_super2()

Expected Output:

12method_super1 method calledmethod_super2 method called

As you can see becuase ChildClass  inherited MySuperClass1 , MySuperClass2 , object of ChildClass  is now able to access method_super1()  and method_super2() .

Overriding methods

To override a method in the base class, sub class needs to define a method of same signature. (i.e same method name and same number of parameters as method in base class).

12345678910111213141516171819classA():  def__init__(self):    self.__x=1  defm1(self):    print("m1 from A")classB(A):  def__init__(self):    self.__y=1  defm1(self):    print("m1 from B")c=B()c.m1()

Expected Output:

1m1 fromB

Here we are overriding m1()  method from the base class. Try commenting m1()  method  in B  class and now m1()  method from Base class i.e class A  will run.

Expected Output:

1m1 fromA

isinstance() function

isinstance()  function is used to determine whether the object is an instance of the class or not.

Syntax: isinstance(object,class_type)

12345678>>>isinstance(1,int)True>>>isinstance(1.2,int)False>>>isinstance([1,2,3,4],list)True

Other Tutorials (Sponsors)

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!