Method Overriding | Run time Polymorphism | Dynamic Binding | Java | Selenium WebDriver

Опубликовано: 18 Сентябрь 2020
на канале: total-qa
263
4

Method Overriding/Run time Polymorphism/Dynamic Binding
======================================
A method in the SuperClass matches with a method in Subclass having same signature
(name, plus the number and the type of its parameters) and return type.
Then Subclass method overrides a method from the SuperClass.

Example:
=========

public class Car
{
public void speed()
{
Syso("max 100 kms");
}
}

public class SportsCar extends Car
{
public void speed()
{
syso("max 200 kms");

}
}
public static void main(String args[])
{
Car c = new SportsCar();
c.speed();

}


A Super Class can hold its subclass objects. It is known as upcasting.

SuperClass s =new SubClass();
Car c = new SportsCar;



But downcasting is not possible in java.

SubClass s = new SuperClass();
SportsCar s = new Car();

The Binding changes from compile to Run time. Then this binding is known as Dynamic Binding.
And the Binding happens during run time, so we call this as runtime polymorphism.