Subscribe to DSC Newsletter

comparative study note of Python and Java 4 --Function and Polymorphism

Function and Polymorphism

The Hovering Question

When I started learning these two languages, as mentioned before, I always keep in mind that the difference between dynamic data type and static data type. Then the question came out: why they need to be like that? What is the advantage of being dynamic data type or being static data type? To make the question more concrete, let’s see how to define a function in both languages.

In Java, everything is very straight forward:

The definition of a function will always contain its returning data type(void if nothing to be returned). And also, the type of the parameters will always be declared. When calling the function if the parameters input are not of the right type, then exception will be raised.

    In Python, everything is simple, yes, simple:

def funtionName(paraName1, paraName2):

        function body

        return returnValue(optional if you need to return something)   


As always stated, there is no data type declaration. Namely, you can input whatever you want as long as the function body action support. And also, you don’t need to state in the header whether the function is returning something and what it will return. You can even set the default value for each parameter which is not supported in Java. A simpler and clearer coding, but for me, it is messier than Java, because it is not safe. You will never know for sure whether the function is working properly. What if you input some parameters of a wrong type?  Most of the time the function will work and return something unexpected.

    What is the pro for this kind of coding style? This is the very question hovering above my mind for a very long time. I asked my professor (who teaches me Java) and she said: “go ask the person who invent it, I don’t know. For me Python is a mess.”  When I asked someone who is introduced to me as an expert in Python and machine learning, I got no answer. She stopped contacting me since I asked this question. Then I knew I asked a very stupid question, though I didn’t know the answer.  



When I learned the term “Guardian”, I thought I found the answer. The definition of guardian is as follow:

“A programming pattern that uses a conditional statement to check for and handle circumstances that might cause an error.”

Then the statements of defining a function in Python will become:

def funtionName(paraName1, paraName2):

        if not isinstance(paraName1, int) or not isinstance(paraName2, int):

            print "error message."

            return None


            function body

            return returnValue  @optional if you need to return something   


The guardian is useful when you want to have the full control on the input data type and the output.  It will make the code in Python similar to that in Java.

But still, this is not the answer to my hovering question.  Python is not invented to act with things complicated to mimic Java.



I keep on searching and learning. Then I learned the concept ‘polymorphism’, and a term ‘generic function’. I know that I find the answer.  A generic function is a function whose behavior depends on the type of the attributes input as parameters.

In Java, a generic function is a set of functions with the same name but different formal parameters.

For example:



 public static int testF(int a, int b){

       return a+b;


 public static boolean testF(boolean a, boolean b){

       return a||b;


When you call the testF function like this


Then A is 5. But :

boolean  A = testF(true, false);

then A is ture.

The static function testF() is a generic function.

But as you can see, testF() is actually two different functions. If we want a function with a hundred of parameter combinations but only one name, then we have to write 100 different codes to define the function. Is it possible that we write only one stack of statements to define a function with 100 possible parameter combinations? This is where the idea ‘polymorphism’ came from.

Polymorphism is the provision of a single interface to entities of different types.

In Java, polymorphism is quite a high level topic which is beyond the scope of this article. It contains 3 subclass:

  1. Function overloading (as shown above)
  2. Parametric polymorphism
  3. Subtyping and inheriting

For more detail you need a text bookJ.

Python is built to be polymorphic. You can easily write parametric polymorphic functions in Python while it’s quite complicated to do this in Java. You cannot overload functions, but by setting the default value and the guardian statements you can do the same thing. Python is an OOP language, thus subtyping and inheriting is also achievable.

Python for Data Analysis while Java for Software Development


Think about the primitive type in Python: int, long, float, boolean, complex, tuple, list, dictionary.

    If only for data analysis, you don’t even need to write a new class. All the above are already enough. Integrating the ease of writing generic function, Python is really convenience for data processing and calculation. But you don’t want to use Python to develop some large scale enterprise applications (as my professor said it would be a mess if you did so).

     Contrary, compare to Python, java is more suitable to develop large scale software. But for data analysis, you have to work a lot before you take action.

And after all, my question is solved.


Views: 2751

Tags: difference, function, generic, java, polymorphism, python


You need to be a member of AnalyticBridge to add comments!

Join AnalyticBridge

On Data Science Central

© 2021   TechTarget, Inc.   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service