Dynamic_method_dipatch
package com.company;
class phone{
public void Showtime(){
System.out.println("12:14 pm Sunday ");
}
public void on(){
System.out.println("Phone on...");
}
}
class smartphone extends phone{
public void on(){
System.out.println("SmartPhone on...");
}
public void greet(){
System.out.println("Welcome you ...");
}
}
public class Dynamic_method_dispatch {
public static void main(String[] args) {
// phone p=new phone();
// p.Showtime();
// p.on();
// smartphone sp=new smartphone();
// sp.greet();
// sp.on();
phone p=new smartphone();//Correct
/*in this one p is the object of smartphone
and will run only the overriding function only
and rest it will run the phone method only
That is the concept of Dynamic method dispatch
*/
// smartphone ph=mew phone();//Incorrect
p.on();//in on it will run to the smartphone method i.e on()
p.Showtime();//Allowed & run the phone method other than the overriding
// p.greet();//Not allowed
/* P->sp so the overriding function and the p only
sp->p (X )
*/
}
}
o/pSmartPhone on... 12:14 pm Sunday
Comments
Post a Comment