Polymorphism Flashcards

1
Q

Consider the following code:

class A{
   A() {  print();   }
   void print() { System.out.println("A"); }
}
class B extends A{
   int i =   4;
   public static void main(String[] args){
      A a = new B();
      a.print();
   }
   void print() { System.out.println(i); }
}

What will be the output when class B is run ?

A

Note that method print() is overridden in class B.

Due to polymorphism, the method to be executed is selected depending on the class of the actual object.

Here, when an object of class B is created, first B’s default constructor (which is not visible in the code but is automatically provided by the compiler because B does not define any constructor explicitly) is called.

The first line of this constructor is a call to super(), which invokes A’s constructor.

A’s constructor in turn calls print().

Now, print is a non-private instance method and is therefore polymorphic, which means, the selection of the method to be executed depends on the class of actual object on which it is invoked.

Here, since the class of actual object is B, B’s print is selected instead of A’s print. At this point of time, variable i has not been initialized (because we are still in the middle of initializing A), so its default value i.e. 0 is printed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Dynamic Polymorphism

A

Dynamic Polymorphism:

Suppose a subclass overrides a particular method of the superclass. 
Let’s say we create an object of the subclass and assign it to the superclass reference. 
Now, if we call the overridden method on the superclass reference then the subclass version of the method will be called.

Example below:

class Vehicle{
    public void move(){
    System.out.println(“Vehicles can move!!”);
    }
}
class MotorBike extends Vehicle{
    public void move(){
    System.out.println(“MotorBike can move and accelerate too!!”);
    }
}
class Test{
    public static void main(String[] args){
    Vehicle vh=new MotorBike();
    vh.move();    // prints MotorBike can move and accelerate too!!
    vh=new Vehicle();
    vh.move();    // prints Vehicles can move!!
    }
}

It should be noted that in the first call to move(), the reference type is Vehicle and the object being referenced is MotorBike.

So, when a call to move() is made, Java waits until runtime to determine which object is actually being pointed to by the reference.

In this case, the object is of the class MotorBike. 
So, the move() method of MotorBike class will be called. 
In the second call to move(), the object is of the class Vehicle. 
So, the move() method of Vehicle will be called.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is achieved Static polymorphism?

A

Static polymorphism in Java is achieved by method overloading

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How is achieved Dynamic polymorphism?

A

Dynamic polymorphism in Java is achieved by method overriding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Static Polymorphism:

A
In Java, static polymorphism is achieved through method overloading.
Method overloading means there are several methods present in a class having the same name but different types/order/number of parameters.

At compile time, Java knows which method to invoke by checking the method signatures.

So, this is called compile time polymorphism or static binding. The concept will be clear from the following example:

class DemoOverload{
    public int add(int x, int y){  //method 1
    return x+y;
    }
public int add(int x, int y, int z){ //method 2
return x+y+z;
}

public int add(double x, int y){ //method 3
return (int)x+y;
}

public int add(int x, double y){ //method 4
return x+(int)y;
} }
class Test{
    public static void main(String[] args){
    DemoOverload demo=new DemoOverload();
    System.out.println(demo.add(2,3));      //method 1 called
    System.out.println(demo.add(2,3,4));    //method 2 called
    System.out.println(demo.add(2,3.4));    //method 4 called
    System.out.println(demo.add(2.5,3));    //method 3 called
    }
}

In the above example, there are four versions of add methods.

The first method takes two parameters while the second one takes three.

For the third and fourth methods, there is a change of order of parameters.

The compiler looks at the method signature and decides which method to invoke for a particular method call at compile time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly