Skip to main content

33.Python Polymorphism

Python Polymorphism
    Polymorphism is one of the best features of Object oriented Programming.When one object can take multiple forms or multiple behaviors,that object is said to exhibit polymorphism.
There are four ways of implementing Polymorphism:
1)Duck typing
2)Method overloading
3)Method overriding
4)Operator overloading

Duck Typing
    Duck typing is one of features used in dynamic typing.In duck typing,the type of class /object is not checked,it checks only whether a specified method is present or not.Duck typing is of belief that if a bird walks like a duck,quakes like a duck,then the bird is a duck.
In simple words,it does not check the type of the class,it returns the output if specified method is present.
Program
class Lion:
    def feature(self):
        print("I have 4 legs")
class cat:
    def feature(self):
        print("cat,also has 4 legs")
for obj in Lion(),cat():
    obj.feature()
Output
I have 4 legs
cat,also has 4 legs
Program
class Lion:
    def feature(self):
        print("I have 4 legs")
class cat:
    def feature(self):
        print("cat,also has 4 legs")
class Human:
    def info(self):
        print("I have 2 legs")
for obj in Lion(),cat(),Human():
    obj.feature()
Output
I have 4 legs
cat,also has 4 legs
AttributeError: 'Human' object has no attribute 'feature'
    
    In the above program,when obj for Human is created,it checks whether the feature() method is present in the Human class,since it is not present,it issues an error.
Python Method Overloading:
    In simpler terms,Method overloading is a feature in which there can be many methods with the same name but the number of arguments it takes is different.Unlike,other Object oriented programming languages like C++,Java,python does not execute Method overloading .Method overloading is slightly different in python.
Need to learn more about python,check out the video below!!
Happy learning!!

Comments

Post a Comment

Popular posts from this blog

Learn c++ in Tamil | Complete guide and tutorial | Beginner to Advance all concepts explained.

hi guys welcome , all the coding used in video are available here : /* hi guys welcome to learn c++ in tamil what is C++ how what software your own program schools,clg,institute.... start 1.interest 2.patience 3.fun 4.practise computer program - app - programming source code - hardware - fb running , executing machine language :  101010 assembly lang : mov h1; high lang : c , c++....java , js , python  eg :  a+b .....1001010  compiler :  human to machine              machine to human  rules :  a+b;  usage:  games, manufacturing , embedded , finance , graphics , etc...  Guide: 1.problem 2.solution - alternative 3.program  - C++ 4.compile  - turboc , ide , codeblocks , cmd 5.result   - library file - linker : #include<iostream.h>    input , output std::cout<<"welcome";   standard character out << " "; primary section : 1.statements   ;  end 2.functions   main() 3.syntax  , syntax error pra

Get Ethical Hacking Project #1

Thanks for your interest in our course This is a paid course, 150 rs for Indian people, 5$ for international people Instructions to get course: Indian people can get the course by paying using this UPI id   tamilhacks@ybl  (KARTHIKEYAN T) send 150 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)     The above mentioned method is most preferable. if you don't have a upi id app, install  phonepe  or  google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100 Rs for tax and other charges here )   After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that . the course is in zip format, so after downloading the file extra

Xplore-Know your TCS Solutions

KNOW YOUR TCS-SOLUTIONS 1.Who is the founder of Tata group? Jamsetji Tata 2.Which characteristics of Big Data deals with trustworthiness of data? Veracity 3.Which is India's first indigeneously designed and manufactured car? Tata Indica 4.What was originally called the "Imitation game" by its creator? The Turing test 5.90% of world's data is created in the last 2 years. True 6.In 1990s,which company did Tata steel acquire? Corus 7.How many independently operating companies are there under Tata group? Over 100 8.We use natural language processing for reviewing twitter fields. True 9.The change of mind set is required from "optimizing scarce resources" to "harnessing abundance" will help in? Leveraging ecosystems 10.Robots can be used for building houses. True 11.Which among the following key for IT management is based on the proper use of data? Embedding security in every system. 12.When did Tata Motors unveil Tata Nano? 2008 13.Business 4.0 brings