Polymorphism, one of the pillar of Object oriented programming, means many forms.
There are two types of polymorphism
Static (don't confuse it with keyword static in java)
Dynamic or runtime
What is the difference between these two?
There are two types of polymorphism
Static (don't confuse it with keyword static in java)
Dynamic or runtime
What is the difference between these two?
(ahem, curtain raising)
Static polymorphism can be considered as concept of overloading.
&
Dynamic polymorphism can be considered as concept of overriding.
Explanation of above mentioned points:
Overloading can be considered as an example of Static polymorphism Overloading means same named methods can act on different signatures.
System.out.println() is perfect example of overloading (applauses), it may take String, Object reference and primitive data types as argument.Fact is that each overloaded method is different and compiler can see this difference at compile time that is why in overloading binding between method definition and method call is done at compile time and it is dependent on reference of class, and we know reference is created at compile time.
Static polymorphism can be considered as concept of overloading.
&
Dynamic polymorphism can be considered as concept of overriding.
Explanation of above mentioned points:
Overloading can be considered as an example of Static polymorphism Overloading means same named methods can act on different signatures.
System.out.println() is perfect example of overloading (applauses), it may take String, Object reference and primitive data types as argument.Fact is that each overloaded method is different and compiler can see this difference at compile time that is why in overloading binding between method definition and method call is done at compile time and it is dependent on reference of class, and we know reference is created at compile time.
Overriding can be considered as an example of Dynamic polymorphism.
Overriding means adding your own functionality to a method in child class, which comes from parent class.
Now the binding between method definition and method call is done at runtime because it is dependent on object of class and objects are created at runtime.
Consider overriding Object.toString() method and giving your own implementation to it.
Now consider java program that initiates series of objects and call to toString() method.
Compiler does not mind difference in object references.
At runtime only object's toString() implementation can be seen.
That is why they are called runtime or dynamic polymorphism.
Lets clear it with the help of an example :
Now the binding between method definition and method call is done at runtime because it is dependent on object of class and objects are created at runtime.
Consider overriding Object.toString() method and giving your own implementation to it.
Now consider java program that initiates series of objects and call to toString() method.
Compiler does not mind difference in object references.
At runtime only object's toString() implementation can be seen.
That is why they are called runtime or dynamic polymorphism.
Lets clear it with the help of an example :
abstract class Car
{
public void showBrakeType()
{
System.out.println("Default Brakes");
}
}
class CarManufacturer1 extends Car
{
public void showBrakeType()
{
System.out.println("Drum Brakes");
}
}
class CarManufacturer2 extends Car
{
public void showBrakeType()
{
System.out.println("Disk Brakes");
}
}
public class Test1
{
public static void main(String a[])
{
Car c1=new CarManufacturer1();
Car c2=new CarManufacturer2();
c1.showBrakeType();
c2.showBrakeType();
}
}
Output will be :
Drum Brakes
Disk Brakes
So above example illustrates that at runtime JVM decides which method to invoke depending on the object ,base class is referring to, i.e. in our example, Car type reference ,referring to two different objects, so different methods being called.
This is called Dynamic Polymorphism.
So up to this was about Static & Dynamic Polymorphism
Now moving ahead what if showBrake() method is static ?
Can we override static method ?
Above example will be like :
Now moving ahead what if showBrake() method is static ?
Can we override static method ?
Above example will be like :
abstract class Car
{
public static void showBrakeType()
{
System.out.println("Default Brakes");
}
}
class CarManufacturer1 extends Car
{
public static void showBrakeType()
{
System.out.println("Drum Brakes");
}
}
class CarManufacturer2 extends Car
{
public static void showBrakeType()
{
System.out.println("Disk Brakes");
}
}
public class Test1
{
public static void main(String a[])
{
Car c1=new CarManufacturer1();
Car c2=new CarManufacturer2();
c1.showBrakeType();
c2.showBrakeType();
}
}
This will compile fine. And when we run the above code with static methods output will be
Default Brakes
Default Brakes
What actually happened is that one static method is hiding another static method.
In case of an instance method, JVM actually determine which method to call, by referring to instance of class (object, c1 or c2 in our case).
But in case of static method, compiler or JVM don't expect any instance (object) to invoke static method, even if we supply instance(object), JVM will ignore it.
JVM will confirm class type of reference variable at compile time and using that class type will call the method.
So in our case class type of our reference variable is Car, so Car.showBrakeType() will be called irrespective of that we are supplying any instance or not.
So it means when we say static methods can't be overridden, it actually means even if we code something that looks like overriding static method, still it won't execute like overridden methods.
That is what we call Data Hiding
So in short when we talk about Overriding :
instance method get overridden.
static method get hidden.
One question that can strike your mind (if not then ask yourself) is that
What is the benefit of dynamic polymorphism ?
What is the benefit of making reference type of super class, referring to object of child class ?
Answer is Flexibility. you can make utility method which will accept super class as parameter type, and hence can pass any child class object as parameter while calling that method. So covering future child classes in that utility method also.
0 nhận xét:
Đăng nhận xét