Polymorphism in python

 Polymorphism:

  • Poly means many and prism means form so polymorphism means many forms.
  • It is used to perform a single action in different ways.
  • For example,
  • Action is drawn and it performance maybe draw a line or draw a circle.

Compile-time polymorphism:

  • The runtime polymorphism is also called static polymorphism.
  • It is the best example is method overriding.

Example program:

  1. #method overriding or runtime polymorphism

    class Shape:

        result=0

        def area(self):

            print("\n\tCalculating area of Rectangle,Circle,Triangle .........")

    class Rectangle(Shape):

        def area(self):

            l=int(input("Enter length: "))

            w=int(input("Enter width: "))

            result=l*w; #area=length*width;

            print("Area of rectangle : ",result)

           

    class Circle(Shape):

        def area(self):

            r=float(input("Enter radius  of circle : "))

            result=3.14*r*r; #area=3.14*radius*radius;

            print("Area of circle : ",result)

     

    s=Shape()

    s.area()

    r=Rectangle()

    r.area()

    c=Circle()

    c.area()

Output:

Note:

Share:

1 comment:

  1. Emperor Humayun was the first to organize them,Call Girls in Ahmedabad but Akbar and his successors made them more elaborate.Call Girls in Ahmedabad The fair was closed for the public,Call Girls in Ahmedabad while the women of the harem, Rajput ladies and the wives and daughters Call Girls in Ahmedabad of the noblemen in the court,Call Girls in Ahmedabad set up their own stalls to sell cloth, Escorts Service in Ahmedabad jewelry, handicrafts, etc.

    ReplyDelete

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me