xxxxxxxxxx
package com.company;
class Phone{
public void showTime(){
System.out.println("Time is 8 am");
}
public void on(){
System.out.println("Turning on Phone...");
}
}
class SmartPhone extends Phone{
public void music(){
System.out.println("Playing music...");
}
public void on(){
System.out.println("Turning on SmartPhone...");
}
}
public class cwh_49_dynamic_method_dispatch {
public static void main(String[] args) {
// Phone obj = new Phone(); // Allowed
// SmartPhone smobj = new SmartPhone(); // Allowed
// obj.name();
Phone obj = new SmartPhone(); // Yes it is allowed
// SmartPhone obj2 = new Phone(); // Not allowed
obj.showTime();
obj.on();
// obj.music(); Not Allowed
}
}
xxxxxxxxxx
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal animal1 = new Dog(); // Reference of type Animal, object of type Dog
Animal animal2 = new Cat(); // Reference of type Animal, object of type Cat
animal1.sound(); // Executes Dog's implementation of sound()
animal2.sound(); // Executes Cat's implementation of sound()
}
}