Differencebetween Dynamic and Static Polymorphism in Java

What is the difference between dynamic and static polymorphism in Java?

Polymorphism

1. Static binding/Compile-Time binding/Early binding/Method overloading.(in same class)

2. Dynamic binding/Run-Time binding/Late binding/Method overriding.(in different classes)

overloading example:

class Calculation {  
void sum(int a,int b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}

public static void main(String args[]) {
Calculation obj=new Calculation();
obj.sum(10,10,10); // 30
obj.sum(20,20); //40
}
}

overriding example:

class Animal {    
public void move(){
System.out.println("Animals can move");
}
}

class Dog extends Animal {

public void move() {
System.out.println("Dogs can walk and run");
}
}

public class TestDog {

public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object

a.move();//output: Animals can move

b.move();//output:Dogs can walk and run
}
}

Confusion over polymorphism concept in java

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.

What is the difference between dynamic and static polymorphism in Java?

Polymorphism

1. Static binding/Compile-Time binding/Early binding/Method overloading.(in same class)

2. Dynamic binding/Run-Time binding/Late binding/Method overriding.(in different classes)

overloading example:

class Calculation {  
void sum(int a,int b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}

public static void main(String args[]) {
Calculation obj=new Calculation();
obj.sum(10,10,10); // 30
obj.sum(20,20); //40
}
}

overriding example:

class Animal {    
public void move(){
System.out.println("Animals can move");
}
}

class Dog extends Animal {

public void move() {
System.out.println("Dogs can walk and run");
}
}

public class TestDog {

public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object

a.move();//output: Animals can move

b.move();//output:Dogs can walk and run
}
}

Can overloading and overriding in java be static polymorphism as well as dynamic polymorphism?

Q1 upcasting: Upcasting and treating a subclass object as if it were a parent class object is the essence of polymorphism. So your question, "What if I don't upcast?" Then you are not using polymorphism

Q2 static polymorphism - method overridding: This is what it's saying:

  • static: static methods are overwritable, since they are associated with the class, not the object. So for instance, if you have a class with a main method, and then you subclass that, and add a main method to the subclass, when you execute the main method in the subclass, it is statically overridden

    • private: private methods are not inherited and cannot be called in the subclass, unless that are statically overridden

    • final: final methods are not overriddable. Therefore, redefining them in a subclass creates a statically overridden method

Is all dynamic binding a kind of polymorphism?

First of all dynamic binding (or late binding) is an improper term. You are talking about dynamic dispatch which is a different thing.

Dynamic binding is choosing which implementation of a method will be called at runtime, but this doesn't happen in Java. But you have dynamic dispatch which is the ability to choose the correct polymorphic implementation of a method at compile time (which usually translates to choosing the most specialized version of the method/function).

But I wouldn't say that dynamic dispatch is polymorphism, I'd say that to support polymorphism you need a mechanism to choose the correct implementation of a method and this is dynamic dispatch.

Inheritance is a kind of polymorphism called subtyping (or subtype polymorphism) so, contrary to what you say, inheritance is always a form of polymorphism, but other kinds of polymorphism exist, think about:

  • ad hoc polymorphism (implemented through overloading) where the same function has different meanings when applied to different arguments
  • parametric polymorphism (implemented with Generics in Java) where you have types with type variables so that a single type can express infinite number of types by binding the variable to a specific type (or a class of types), think about List<Integer>

Static binding and dynamic binding in Java

Trying to sum up discussion. Edit as required.

Static binding in Java occurs during Compile time while Dynamic binding occurs during Runtime.

At compile time, p1 and p2 both are types of Parent and Parent has doSmth(Object) method hence both lines binds to the same method.

    p1.doSmth("String");
p2.doSmth("String");

During runtime, dynamic binding comes into picture.

p1 is instance of Child1 and Child1 has overridden doSmth(Object) hence the implementation from Child1 is used.

p2 is instance of Child2 and Child2 does NOT override doSmth(Object) hence the implementation of inherited method from Parent is called.



Related Topics



Leave a reply



Submit