Hi friends

If we will override nonstatic methods or if we will overload non static methods means shall we say it is the run time polymorphism? Is that so how to give example for compile time polymorphism in java?

If i will override static method means, friends are saying it is not compile time polymorphism, It is method hiding.

I have some confusion with runtime polymorphism and compile time polymorphism in java.
I have refered some sites related to this. But i didnt get satisfy with those.

I want to know whether my thought about polymorphism is right or wrong?
If i will get any clear answers or any good site links to demonstrate about compiletime and runtime polymorphism means, that will be helpful fo me.

Thnks in advance!

Overloading is compile-time polymorphism because it is during compile time that the JVM figures out what method to call by checking what was passed in the arguments to the overloaded method.

//depending on what was passed as the arguments the right method is chosen at compile time
public int add(int a, int b){}
public int add(float a, float b){}

Thus overriding is run-time polymorphism because during run-time two or more objects of different types(super and sub classes) can have overridden the method, thus depending on the object the appropriate method is chosen at run-time.

public class Top{
public void add(int a, int b){}
}

public class Bottom extends Top{
public void add(int a, int b){}
}

Because now two types of objects(Top or Bottom) can reference the same method, it is during run-time that depending on the object the right method is chosen.

Overriding a method hides the method of the superclass, in the example above, whenever you make a call to the add method in the Bottom class the overridden method gets invoked. The same concept applies to fields, if the class Top had a field named "topField" and the Bottom class declares a field of the same type(class type) with the same name, the field inside Bottom will hide the Field inside Top. The only way to access the field in the superclass is to use the "super" keyword or access the field through a getter method.

commented: Well written; good example. +2

Overloading is compile-time polymorphism because it is during compile time that the JVM figures out what method to call by checking what was passed in the arguments to the overloaded method.

Here my doubt is "Method binding will happen at runtime, because for object creation we are using new keyword(funtion of new is to create object at run time), So by using object reference only we can call nonstatic methods. So how we can say that it is compile time polymorphism?"

It is compile-time polymorphism because when code like this "Sring a = new String("hello")" gets compiled the compiler figures out by the arguments passed what constructor to call.

Yes the creation of the object happens at run-time(when that line of code is reached) but it is not run-time polymorphism because constructors cannot be overridden, thus because only in that one class is that constructor declared the compiler figures out at compile-time which constructor to call.

The bottom line is that method overriding is run-time polymorphism because it is at run-time when the JVM figures out which method to call depending on the type of object it is using to call that method. And that method overloading is compile-time because it is at compile time that the right method gets chosen depending on the arguments passed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.