简体   繁体   中英

Confusion over polymorphism concept in java

i am bit confused over polymorphism concept in java because of different authors writing it differently.

Case -1

Dynamic (run time) polymorphism---Method overloading and method overriding using instance methods are the examples for dynamic polymorphism.

Static (compile time) polymorphism ---Method overloading and method overriding using static methods; method overriding using private or final methods are examples for static polymorphism

Case 2

method overloading is an example of compile time/static polymorphism

method overriding is an example of run time/dynamic polymorphism

so which case is correct ??? and java supports static or dynamic polymorphism ?

Overloading is at compile time. Overriding is at runtime time. So the case 2 is correct.

Case 2 is correct

Method overriding - run time polymorphism

Method overloading - compile time polymorphism

Compile Time and Runtime Polymorphism is directly related to when the calls are resolved .

In compile Time Polymorphism, the calls are resolved during compile time. Method over loading is an example of Compile time Polymorphism. Overloading is irrespective of whether it is at instance level or class level

Example :

 public class Sample {
    static void doSomething(InputStream is) {
        System.out.println("is");
    }

    static void doSomething(OutputStream os) {
        System.out.println("os");
    }

    public static void main(String args[]) {
        System.out.println(doSomething(null)); // "compilation" error . Ambiguous call. Both InputStream and OutputStream can take `null` argument.
    } 

}

Next, Runtime Polymorphism : Here the calls / method signatures are checked for existence during compile time but the actual calls are resolved during runtime. Example :

class ParentOfSample {
    void testingOverriding(String s) {
        System.out.println("Parent..");
    }
}

public class Sample extends ParentOfSample {
    static void doSomething(InputStream is) {
        System.out.println("is");
    }

    static void doSomething(OutputStream os) {
        System.out.println("os");
    }

    void testingOverriding(String s) {
        System.out.println("Sample..");
    }

    public static void main(String args[]) {
        ParentOfSample s = new Sample();
        s.testingOverriding(null);
    }
}

O/P : Sample. Note that during Overriding the method signatures are the same.

So, the bottom line is : The second case is right. Java supports both static and dynamic polymorphism.

Basically Method Overloading is Static binding you can also say as Early binding. because compiler can identify at compile time only that which method you actually want to call.

Method Overriding is dynamic binding or late binding where JVM will only identify method call at run time (depends on Base class reference pointing to which Child class object).

You can check this for Method OverLoading :

class OverLoadedClass{
int x;
int y;
public void display(int x)
  {
    System.out.println(x);    
  }
public void display(int x,int y)
  {
    System.out.println(x+""+y);
  }
 }
 public class Test{
     public static void main(String args[]){
    OverLoadedClass o= new OverLoadedClass();
    o.display(5);
    o.display(5,10);
}
}

You can check this for Method Overriding :

class Base{
int x;
public void display(int x)
 {
  System.out.println(x);
 }
}
class Child extends Base{
int x;
public void display(int x)
 { 
   System.out.println(x);
  }
}
public class Test{
public static void main(String args[]){
    Base o= new Child();
    o.display(5);
}
}

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