0

Why method overloading called as static or compile-time polymorphism

sample in Java.

class StaticPolymorphismSample {
    void polymorphicMethod(int a) {

    }
    void polymorphicMethod(int a, int b) {

    }
    void polymorphicMethod(String a) {

    }
    void nonPolymorphicMethod(int a) {

    }
    void nonPolymorphicMethod1(int a) {

    }

}

so my question is.

Why we say that method overloading ( in this case polymorphicMethod methods ) are static polymorphism , but another methods( nonPolymorphicMethod(int a) nonPolymorphicMethod1(int a) ) are not polymorphism.

technically I cannot see different between method with same name and different parameters and method with different, all answers in here and topics in google is not applicable for my question.

Michael
  • 41,989
  • 11
  • 82
  • 128
Hayk Melkonyan
  • 2,110
  • 4
  • 16
  • 22
  • The difference is that `add(double, double)` and `add(int, int)` are clearly different *forms* ('polymorphism' meaning 'many forms') of the same function. `add(int, int)` and `subtract(int, int)` are not. – Michael Jul 30 '19 at 14:06
  • why `add(int, int)` and `anotherAdd(int, int)` are not ? we can say they are different forms too, for example, `list.add(element)` and `list.addAll(element,element)` they have same meaning in different forms. – Hayk Melkonyan Jul 30 '19 at 14:18
  • Because doing so is not meaningful. Ad hoc polymorphism (overloading) must be resolved at compile time. Subtype polymorphism (inheritance) must be resolved at runtime. You could make the case that your *specific* example is a kind of "polymorphism" but it is not generalizable and so is not a useful classification to have. – Michael Jul 30 '19 at 14:24
  • @NathanHughes please remove duplicate mark from question, or add some correct answer link, which will directly answer to my question. – Hayk Melkonyan Jul 30 '19 at 14:27
  • Why would you ever have two functions that do the same thing with the same parameters? If you need two different functions that add in different cases you would either overload it, or extend the class and implement different functionality for different objects. Your example makes no sense @Hayk Melqonyan. Find an example in a library in the real world that does what you say. – Nexevis Jul 30 '19 at 14:29
  • @Michael thanks for answer. so in my opinion difference between `list.add(element)` , `list.addAll(element,element)` and `add(int, int) ` , `add(double, double)` are only same nameing, they both work on compile time, and both are many forms of one work – Hayk Melkonyan Jul 30 '19 at 14:30
  • @Nexevis I don't need two functions that will do same thing. I need two functions which will do similar things , they can have different names or different params. what I cannot understand , what is advantage of overloaded methods from methods with different name, I can find only the same names, technically 2 overloaded methods and 2 different methods are the same. real example from Java. `List.add(T t)` and `List.addAll(Collection c)` they do similar things , this is not overloading, but you can do same thing with overloading, so this is not static-polymorphism, why? – Hayk Melkonyan Jul 31 '19 at 06:01

1 Answers1

0

For nonPolymorphicMethod1(int a) the reason this wouldn't be considered polymorphic is because it has a different name from the other nonPolymorphicMethods.

For nonPolymorphicMethod( int a, int b ) and nonPolymorphicMethod( int a ) these aren't considered polymorphic as they don't take the same parameters. Edit This is Wrong See Next Line

The other methods you've shown are polymorphic due to their sharing of a name, but differing parameter types or number of parameters.

A better example of polymorphism in methods would be :

public abstract class ClassA
{
    public Object getObject()
    {
        return new Object();
    }
}

public class ClassB extends ClassA
{
    @Override
    public ClassB getObject()
    {
        return new ClassB();
    }
}

public class ClassC extends ClassA
{
    @Override
    public ClassC getObject()
    {
        ClassC example = new ClassC();
        example.doStuff();
        return example;
    }

    private void doStuff()
    {
        // Do Something To Change The Object 
    }
}
Glomdor
  • 1
  • 1
  • His example is a correct use of _compile_ time polymorphism. Your example is _runtime_ polymorphism. But I agree with why the nonPolymorphic method is not polymorphic. – Nexevis Jul 30 '19 at 14:08
  • The top answer [here](https://stackoverflow.com/questions/20783266/what-is-the-difference-between-dynamic-and-static-polymorphism-in-java) demonstrates the difference well. – Nexevis Jul 30 '19 at 14:11
  • @Nexevis you are right, this answer is about dynamic polymorphis. – Hayk Melkonyan Jul 30 '19 at 14:14
  • your mentioned link doesn't explain why my nonPolymorphicMethod is not part of static-polymorphism, for overloaded methods only difference is that they have same name, but technically I think it is not important they have same name or different names – Hayk Melkonyan Jul 30 '19 at 14:15
  • this answer does point out the methods that are not static-polymorphic have different names, which does answer the question, there is no kind of polymorphism going on if the names are different. not getting why this is downvote-worthy. – Nathan Hughes Jul 30 '19 at 14:20
  • @NathanHughes first of all this question is not duplicate, I have updated why. this answer explains dynamic polymorphis(not static) , mention of different nameing is nor applicable answer for me , because it is not technical description. – Hayk Melkonyan Jul 30 '19 at 14:23
  • @Nathan Hughes I didn't downvote the answer, but it does incorrectly state that the methods marked as polymorphic in the post are not polymorphic, when they are. – Nexevis Jul 30 '19 at 14:26
  • @Nexevis: right, the answer is using "polymorphism" differently from OP. – Nathan Hughes Jul 30 '19 at 14:28
  • @Nathan Hughes It is not "different" this line is flat out wrong `these aren't considered polymorphic as they don't take the same parameters.` – Nexevis Jul 30 '19 at 14:30
  • @NathanHughes I want to ask you one more time , please remove duplicate from this question, your mentioned answer is different, it is explanation of difference between dynamic and static polymorphism, my question is about explanation static polymorphism by itself. – Hayk Melkonyan Jul 31 '19 at 06:07
  • @NathanHughes I have removed downvote, but answer still is not correct . its all about dynamic polymorphism. – Hayk Melkonyan Jul 31 '19 at 06:09