|
| 1 | +# Dynamic Method Dispatch in Java |
| 2 | +- Dynamic method dispatch is also known as run time polymorphism. |
| 3 | +- It is the process through which a call to an overridden method is resolved at runtime. |
| 4 | +- This technique is used to resolve a call to an overridden method at runtime rather than compile time. |
| 5 | +- To properly understand Dynamic method dispatch in Java, it is important to understand the concept of upcasting because dynamic method dispatch is based on upcasting. |
| 6 | + |
| 7 | +### Upcasting : |
| 8 | +- It is a technique in which a superclass reference variable refers to the object of the subclass. |
| 9 | +- Example : |
| 10 | + |
| 11 | +``` |
| 12 | +class Animal{} |
| 13 | +class Dog extends Animal{} |
| 14 | +Animal a=new Dog();//upcasting |
| 15 | +``` |
| 16 | + |
| 17 | +- In the above example, we've created two classes, named Animal(superclass) & Dog(subclass). While creating the object 'a', we've taken the reference variable of the parent class(Animal), and the object created is of child class(Dog). |
| 18 | + |
| 19 | +### Example to demonstrate the use of Dynamic method dispatch : |
| 20 | +- In the below code, we've created two classes: Phone & SmartPhone. |
| 21 | +- The Phone is the parent class and the SmartPhone is the child class. |
| 22 | +- The method on() of the parent class is overridden inside the child class. |
| 23 | +- Inside the main() method, we've created an object obj of the Smartphone() class by taking the reference of the Phone() class. |
| 24 | +- When obj.on() will be executed, it will call the on() method of the SmartPhone() class because the reference variable obj is pointing towards the object of class SmartPhone(). |
| 25 | + |
| 26 | +``` |
| 27 | +class Phone{ |
| 28 | + public void showTime() { |
| 29 | + System.out.println("Time is 8 am"); |
| 30 | + } |
| 31 | + public void on(){ |
| 32 | + System.out.println("Turning on Phone..."); |
| 33 | + } |
| 34 | +} |
| 35 | +
|
| 36 | +class SmartPhone extends Phone { |
| 37 | + public void music() { |
| 38 | + System.out.println("Playing music..."); |
| 39 | + } |
| 40 | + public void on() { |
| 41 | + System.out.println("Turning on SmartPhone..."); |
| 42 | + } |
| 43 | +} |
| 44 | +
|
| 45 | +public class CWH { |
| 46 | + public static void main(String[] args) { |
| 47 | +
|
| 48 | + Phone obj = new SmartPhone(); // Yes it is allowed |
| 49 | + // SmartPhone obj2 = new Phone(); // Not allowed |
| 50 | +
|
| 51 | + obj.showTime(); |
| 52 | + obj.on(); |
| 53 | + // obj.music(); Not Allowed |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +``` |
| 59 | +Output : |
| 60 | +Time is 8 am |
| 61 | +Turning on SmartPhone... |
| 62 | +``` |
| 63 | + |
| 64 | +**Note:** The data members can not achieve the run time polymorphism. |
| 65 | + |
| 66 | +### Code as described/written in the video : |
| 67 | + |
| 68 | +``` |
| 69 | +class Phone { |
| 70 | + public void showTime() { |
| 71 | + System.out.println("Time is 8 am"); |
| 72 | + } |
| 73 | + public void on() { |
| 74 | + System.out.println("Turning on Phone..."); |
| 75 | + } |
| 76 | +} |
| 77 | +
|
| 78 | +class SmartPhone extends Phone { |
| 79 | + public void music() { |
| 80 | + System.out.println("Playing music..."); |
| 81 | + } |
| 82 | + public void on() { |
| 83 | + System.out.println("Turning on SmartPhone..."); |
| 84 | + } |
| 85 | +} |
| 86 | +public class cwh_49_dynamic_method_dispatch { |
| 87 | + public static void main(String[] args) { |
| 88 | + // Phone obj = new Phone(); // Allowed |
| 89 | + // SmartPhone smobj = new SmartPhone(); // Allowed |
| 90 | + // obj.name(); |
| 91 | +
|
| 92 | + Phone obj = new SmartPhone(); // Yes it is allowed |
| 93 | + // SmartPhone obj2 = new Phone(); // Not allowed |
| 94 | +
|
| 95 | + obj.showTime(); |
| 96 | + obj.on(); |
| 97 | + // obj.music(); Not Allowed |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +**Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-49/JavaChapter10.pdf)** |
| 103 | + |
| 104 | +**Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-49/UltimateJavaCheatSheet.pdf)** |
0 commit comments