Dynamic Dispatch
The process of assigning subclass object to superclass reference variable is called as dynamic dispatch.
Which of the following are valid?
A ob=new A();// Valid
A ob=new B();// Valid
A ob=new D();// Valid
B ob=new B();// Valid
B ob=new A();// Invalid
B ob=new C();// Invalid
B ob=new D();// Valid
D ob=new A();// Invalid
A ob=new E();// Valid
3.10 Polymorphism
- Polymorphism (One name - Many forms).
- Polymorphism is the ability of an object to behave differently at different situations.
Types of Polymorphism
- Static Polymorphism
- Dynamic Polymorphism
1) Static Polymorphism
Static polymorphism can be achieved using method overloading.
- It can be achieved with both instance and static methods.
- Static Polymorphism is also called as compile time polymorphism or early binding because Java compiler is responsible to bind the method calls with actual method at compile time.
- Java compiler will verify the method name first then method parameters to verify the matching method.
- Return type will not be considered.
class Arithmetic{
void sum(double d){ ... }
void sum(int a,boolean b){ ... }
}
Arithmetic ar=new Arithmetic();
1.ar.sum(10);
2.ar.sum(10,20);
3.ar.sum(10.0);
Case 1 : ar.sum10()
Compiler will do the following tasks:
sum10()method will be verified in the class.sum10()method is not available, so compile time error will be given.
Case 2 : ar.sum(10, 20)
Compiler will do the following tasks:
sum()method will be verified in the class.sum()method is available.- Now method parameters will be verified.
- Method with given parameters is not available. So compile time error will be given.
Case 3 : ar.sum(10.0);
Compiler will do the following tasks:
sum()method will be verified in the class.sum()method is available.- Now method parameters will be verified.
- Method with given parameters is available.
- Compiler will bind this method call to actual matched method and it will go to next line.
2) Dynamic Polymorphism
- Dynamic Polymorphism can be achieved using both method overriding and dynamic dispatch.
- It is also called as runtime polymorphism or late binding because JVM is responsible to decide which method will be invoked based on actual object available in reference variable.
- It can be achieved only with instance method, can't be achieved using static method.
Person.java
class Person {
void eating() {
System.out.println("Person -> eating()");
}
void walking() {
System.out.println("Person -> walking()");
}
static void sleeping() {
System.out.println("Person -> sleeping()");
}
}
Employee.java
class Employee extends Person {
void walking() {
System.out.println("Employee -> walking()");
}
static void sleeping() {
System.out.println("Employee -> sleeping()");
}
void working() {
System.out.println("Employee -> working()");
}
}
Student.java
class Student extends Person {
void walking() {
System.out.println("Student -> walking()");
}
static void sleeping() {
System.out.println("Student -> sleeping()");
}
void reading() {
System.out.println("Student -> reading()");
}
}
Lab518.java
class Lab518 {
public static void main(String args[]) {
Person pob = null;
pob = new Student();
pob.eating();
pob = new Employee();
pob.eating();
}
}
Lab519.java
class Lab519 {
public static void main(String args[]) {
Person pob = null;
pob = new Student();
pob.walking();
pob = new Employee();
pob.walking();
}
}
Lab520.java
class Lab520 {
public static void main(String args[]) {
Person pob = null;
pob = new Student();
pob.sleeping();
pob = new Employee();
pob.sleeping();
}
}
Lab521.java
class Lab521 {
public static void main(String args[]) {
Person pob = null;
pob = new Student();
pob.reading();
pob = new Employee();
pob.working();
}
}
Lab522.java
class Lab522 {
public static void main(String args[]) {
Person pob = null;
pob = new Student();
Student stu = (Student) pob;
stu.reading();
pob = new Employee();
Employee emp = (Employee) pob;
emp.working();
}
}
Lab523.java
class Lab523 {
public static void main(String args[]) {
A aobj = new B();
System.out.println(aobj.x);
}
}
class A {
int x = 10;
}
class B extends A {
String x = "JavaWorld";
}
Output
10
Lab524.java
class Lab524 {
public static void main(String args[]) {
A aobj = new B();
aobj.x = 99;
System.out.println(aobj.x);
}
}
class A {
int x = 10;
}
class B extends A {
String x = "JavaWorld";
}
Output
99
Lab525.java
class Lab525 {
public static void main(String args[]) {
B bobj = new B();
bobj.show();
}
}
class A {
int x = 10;
}
class B extends A {
String x = "JavaWorld";
void show() {
System.out.println(x);
System.out.println(super.x);
}
}
Output
JavaWorld
10
Lab526.java
class Lab526 {
public static void main(String args[]) {
B bobj = new B();
bobj.show();
}
}
class A {
int x = 10;
}
class B extends A {
String x = "JavaWorld";
void show() {
B ref1 = this;
A ref2 = this;
System.out.println(ref1.x + "\t" + ref2.x);
}
}
Output
JavaWorld 10
Lab527.java
class Lab527 {
public static void main(String args[]) {
B bobj = new B();
bobj.x = "SRI";
A aobj = bobj;
aobj.x = 88;
bobj.show();
}
}
class A {
int x = 10;
}
class B extends A {
String x = "JavaWorld";
void show() {
System.out.println(x);
System.out.println(super.x);
}
}
Output
SRI
88