简体   繁体   中英

Polymorphism in Python VS Polymorphism in JAVA

I'm trying to understand the polymorphism in python. Gone through lot of article but there is one doubt still there in my mind. When I compare with java it is little bit confusing for me in Python.

As per my knowledge polymorphism is "one thing in many forms". Polymorphism can be demonstrated using operator overloading, and method overloading. Let's take method overloading as a means to demonstrate Polymorphism concept. In java we can write it without use of inheritance. See below code.

 public class Main{
    public static void main(String[] args) {
        System.out.println(add(1,2));;
        System.out.println(add(1,2,3));
    }
    public static int add(int a,int b){
        return a+b;
    }
    public static int add(int a,int b,int c){
        return a+b+c;
    }   
}

Python code:

class TestPolymorphism:
    def add(self,a,b):
        return (a+b)
    def add(self,a,b,c):
        return (a+b+c)

obj = TestPolymorphism()
print(obj.add(1,2)) #will get an error
print(obj.add(1,2,3))

The same method overloading works in java but not in python. Why there is difference?. If I want it to work then I have to change my python code as below:

class TestPolymorphism:
    def add(self,a,b,c=None):
        if c ==None:
            sum  = a+b
            return sum
        else:
            sum = a+b+c
            return sum

obj = TestPolymorphism()
print(obj.add(1,2))
print(obj.add(1,2,3))

I'm not getting convinced that above the code is an example of Polymorphism. There are article like this not giving me convincing point.

can anyone give me theory behind the polymorphism in python?

The way I have always understood this:

Polymorphism is a concept that basically says it looks like the same but does things differently, depending on context.

  1. if you have a method of the same name which has a different amount of arguments, this is method overloading.

  2. if you have a method of the same name with the same amount of arguments, but does a different thing depending on the class, this is method overriding.

Note: I say amount of arguments, as for dynamically typed languages, the type does only matter at run time. For Java, for instance, it is not only the number of arguments, but also the type, of course.

Both are concrete ways to deal with polymorphism. In detail, depending on the language you are using, the concept might be implemented and sometimes even interpreted differently.

By what i understand in java even you have the same name of the method, you have different method, while in python when you have:

class TestPolymorphism:
def add(self,a,b):
    return (a+b)
def add(self,a,b,c):
    return (a+b+c)

the last add that you write override the last ones.

In conclusion, when you have add with different arguments the best option add(self, a,b,c==None, ..., n=None) and if...

Well from my understanding of polymorphism, it means one thing in many forms, and that can be illustrated using user app interface. A python code could be implemented such that at runtime, a draw method could be used to change the user app interface from drop-down list to textbox. etc. def draw(controls): for control in controls: control.draw I hope you get the point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM