Java Dynamic Method Dispatch | Java tutorials for beginners
Java Dynamic Method Dispatch | Java tutorials for beginners Links: To learn about basics of Java click here π https://www.youtube.com/playlist?list=PLTSNjybYuOXsW0aRk4iI54ncim2qe3rJb Method Overriding: https://youtu.be/FAOKRt9ELAY Polymorphism: https://youtu.be/9VdhiTWfupA Java Inheritance: https://youtu.be/rHFQe_lUPvg Notes: 1)What is (DMD)Dynamic Method Dispatch? 2)Advantage of DMD. When we call methods in a program then which method has to be executed is first handle by compiler at compile time and envoke or execute the method as per given method call And if compiler couldn't handle it at compile time then Jvm handle that method call at run time.. That means, If compiler couldn't understand which method has to be executed for given method call then Jvm resolved that method call at run time and if jvm determine or resolved which method has to be execute at run time it's nothing but run time polymorphism and run time polymorphism is resolved through Dynamic method dispatch. That means, dynamic method dispatch is used to perform run time polymorphism.. Let's understand through example.. // Parent class class Phone { void screen sizes(){ s.o.p("screen size"); } //Child class class Iphone1 extends Phone { void screen sizes(){ s.o.p("IPhone1 has 4.5 inch screen size"); } //Child class class Iphone2 extends Phone{ void screen sizes(){ s.o.p("IPhone2 has 5 inch screen size"); } //Child class class Iphone3 extends Phone{ void screen sizes(){ s.o.p("iPhone has 5.5 inch screen size"); } } public class Test{ public static void main(String [] args){ //Here, parent class refference (phObj )pointing to Iphone1 object.. Phone phObj= new IPhone1();// Upcasting phObj.screenSized(); //Here, parent class refference (phObj )pointing to Iphone2 object.. phObj= new IPhone2(); phObj.screenSized(); //Now, parent class refference (phObj )pointing to Iphone3 object.. phObj= new IPhone3(); phObj.screenSized(); } } In above example,we have create one parent class(Phone Class) and three child class (Iphone1,Iphone2 and Iphone3) and override parent class's screenSized() into all child classes so here screenSized() in Iphone1,Iphone2 and Iphone3 class will be overriding method and then we have create parent class refference variable (Phone phObj;) to hold child class object (new Iphone1();) and it's called as Upcasting. And using Upcasting we can implement Dynamic Method Dispatch. Now, let's understand how? Using parent class refference (phObj) we have called screenSized() now question comes that which screenSized() method get execute. So here Iphone1 class method get execute because run time object (new Iphone1) is of Iphone1 class's object i.e Iphone1 class object is going to create at run time and object creation always takes place at run time and hence which screenSized() method i.e overriding method will execute is decided or resolve at run time. So basically,which overriding method has to be executed or dispatch is decided or resolved dynamically i.e at run time and hence this mechanism is called as Dynamic Method Dispatch. Same we have called Iphone2 class's method and Iphone3 class 's method using parent class refference(phObj) and we have got expected output as follow, Output: Iphone1 has 4.5 inch screen size Iphone2 has 5 inch screen size Iphone3 has 5.5 inch screen size Advantages: 1) DMD is used to perform run time polymorphism.. 2)DMD allow child classes to define their own implementation. 3)DMD allows a class to define methods that will be shared for all its child classes. For more details you can watch video. --------------------End of Notes--------------- Thank you so much..!π #dynamicmethiddispatchinjava #javadynamicmethoddispatchwithreallifeexample #javadynamicmethoddispatchwithnotes #sgtutorial #javaforbeginners
Download
1 formatsVideo Formats
Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.