Dynamic method dispatch is the mechanism by which the call to an overridden method is resolved at runtime rather than compile-time. This occurs when a superclass reference variable is used to refer to a subclass object, and the overridden method is called on that reference. The method to be executed is determined based on the actual object's type, not the reference variable's type.
I’ve entered in 8th year of my corporate journey after passing out in 2015. I started now learning java with your amazing tutorials so that i can switch my domain in IT.
You said we don't know at the compile time which method will be called, But we know right, the method inside the class whose object is actually created, will be executed. So how exactly is this run time polymorphism.
Very interesting. Thank you very much. I'd like to know now: what element is polymorphic now: the variable obj? The method show()? The type? The Object?
The concept of dynamic method dispatch makes the show() method to be polymorphic. Ie. Polymorphism is one interface multiple method .. By providing same method name in all child classes we are making it as an single interface which can operate as multiple method based on instances of class Real time scenario : If we have new implementation (new classes that implements super classes) for some old existing classes we can migrate it smoothly without having to break the code (ie. If we had used superclass reference variable to refer to the object, we can still pass the object of new implementation without need to worry about code breaking)
Lets say there is a class A and class B (without inheritance), so to create objects we say { A obj = new A(); B obj2 = new B() }, now obj and obj2 are variables referring to objects (but what it actually holds is an address location), does it mean different classes have different type of address location?
When i give method name as show1() in class B and method name as show2() in class C and when I create object for B and and try to access the particular methods of particular classes it is not working. but it is working only when all the method names in all classes is show(). this is how i called method in B class obj=new B(); obj.show1(); @Telusko Please give me clarity on this, I was bit confused
This video works only with method overriding. When you create an object of child class with reference of parent class You can use only parent class methods
Is it mandatory to define a main method with in show method of class A i got bit confusing y i should call main method in class A rather then we call main method in our demo class is it acceptable Give me the valid ans any one.....
is it different to C++ because in C++, if we create a pointer of base class and pointed it to derived class object then if we run any function using that pointer it will run function of base class and not of derived class , completely opposite to this tutorial is i am right?
I thought my doubt would finally resolve but no class A{ public void show(){} } class B extends A{ Public void hi(){} } if we create a ref of parents to obj of child is there any way i can access child class methods A obj= new B(); obj.hi(); //thissss
It's working fine like: Vehicle c = new Vehicle(); c.showName(); c = new Car(); c.showName(); But shows an error: Type mismatch: cannot convert from Vehicle to CarJava(16777233) Car c = new Car(); c.showName(); c = new Vehicle(); c.showName(); --------------------------------------------------- Vehicle is the parent class....
we can create reference of parent class and points it to object of child class like what u do in 1st case but we can't do opposite like we create reference of child class and points it to parent class that's why give an error in 2nd case class Phone{ public void name() { System.out.Println("I am phone"); } } Class SmartPhone extends Phone{ public void name() { System.out.Println("I am Smartphone"); } } public demo{ public static void main(string[] args){ Phone obj = new SmartPhone(); // this is correct we can call any smarphone as a phone (Phone obj ) is reference of parent class and new SmartPhone() is object of child class and this can be done but SmartPhone obj = new Phone(); //this is incorrect because we can't say any phone as smartphone as (SmartPhone obj ) is reference of child class and new Phone() is object of parent class and this will throw an error } }
Really you deserve a Dhrona Charya award sir. I've learn a looooooooot from you than I had learnt from my college.
Dont flex too much. It ll be a loooooooooooot of problem for u in future
That's too over bro 😂
@@BLG120bro be humble
Sir you have God gifted knowledge no one can explain this clear thank you.
The best JAVA tutorial series ever
Dynamic method dispatch is the mechanism by which the call to an overridden method is resolved at runtime rather than compile-time. This occurs when a superclass reference variable is used to refer to a subclass object, and the overridden method is called on that reference. The method to be executed is determined based on the actual object's type, not the reference variable's type.
As am a Java Trainee Trainer Telusko videos are 💎
Great place to learn or brush up java. Keep doing this great work sir.
new subscriber..... Sir your videos are too easy to understand and right now I have completed some main topics which will help me in exams thank you
I’ve entered in 8th year of my corporate journey after passing out in 2015. I started now learning java with your amazing tutorials so that i can switch my domain in IT.
Welcome aboard! You're in the right place to level up your skills.
easy to understand! you are my saver!!
You said we don't know at the compile time which method will be called, But we know right, the method inside the class whose object is actually created, will be executed. So how exactly is this run time polymorphism.
I think maybe he meant compiler does not know
It is like episode of any web series.
I am excited to watch next episode after watching previous lacture.
Really too good sir .
Practical and precise explanation
The best explaination ever in few minutes. Thanku sir for the best explanation.
Very interesting. Thank you very much. I'd like to know now: what element is polymorphic now: the variable obj? The method show()? The type? The Object?
The concept of dynamic method dispatch makes the show() method to be polymorphic. Ie. Polymorphism is one interface multiple method .. By providing same method name in all child classes we are making it as an single interface which can operate as multiple method based on instances of class
Real time scenario : If we have new implementation (new classes that implements super classes) for some old existing classes we can migrate it smoothly without having to break the code (ie. If we had used superclass reference variable to refer to the object, we can still pass the object of new implementation without need to worry about code breaking)
Lets say there is a class A and class B (without inheritance), so to create objects we say { A obj = new A(); B obj2 = new B() }, now obj and obj2 are variables referring to objects (but what it actually holds is an address location), does it mean different classes have different type of address location?
yes
3:16 this is a heterogeneous object creation right?
superb explanation Sir thank you!
Very Clear Understanding For Me...
that was an beautiful explanation. you earned a sub
Great Explanation
Sir will this work if B extends A and C extends B. Is it possible to call show method of class A from C
yes its possible
When i give method name as show1() in class B and method name as show2() in class C and when I create object for B and and try to access the particular methods of particular classes it is not working. but it is working only when all the method names in all classes is show(). this is how i called method in B class
obj=new B();
obj.show1();
@Telusko Please give me clarity on this, I was bit confused
This video works only with method overriding.
When you create an object of child class with reference of parent class
You can use only parent class methods
Thanks a lot sir for ez understanding series💖
Then what is the difference between function overriding and polymorphism sir?
The example of laptop and computer is best.
Excellent explanation, I just noticed you look a little bit like Nacho from Better Call Saul :)
Is it mandatory to define a main method with in show method of class A i got bit confusing y i should call main method in class A rather then we call main method in our demo class is it acceptable
Give me the valid ans any one.....
Thank you sir❤
is it different to C++ because in C++, if we create a pointer of base class and pointed it to derived class object then if we run any function using that pointer it will run function of base class and not of derived class , completely opposite to this tutorial is i am right?
Yes you are correct. I was looking a comment which had the same query. Finally found one guy !! cheers
Thank You
Thanks a lot man
I thought my doubt would finally resolve but no
class A{
public void show(){}
}
class B extends A{
Public void hi(){}
}
if we create a ref of parents to obj of child
is there any way i can access child class methods
A obj= new B();
obj.hi(); //thissss
that's exactly how you do.
Where are u from sir?
Runtime polymorphism or Dynamic Method Dispatch
Yogiji : Aj se tumhara naam taklusko
you mean you are calling BShow? Like Bigshow!
7:13
once we have gone through*
class A{
}
❌
class B
{
}
✅
It's working fine like:
Vehicle c = new Vehicle();
c.showName();
c = new Car();
c.showName();
But shows an error: Type mismatch: cannot convert from Vehicle to CarJava(16777233)
Car c = new Car();
c.showName();
c = new Vehicle();
c.showName();
---------------------------------------------------
Vehicle is the parent class....
we can create reference of parent class and points it to object of child class like what u do in 1st case but we can't do opposite like we create reference of child class and points it to parent class that's why give an error in 2nd case
class Phone{
public void name()
{
System.out.Println("I am phone");
}
}
Class SmartPhone extends Phone{
public void name()
{
System.out.Println("I am Smartphone");
}
}
public demo{
public static void main(string[] args){
Phone obj = new SmartPhone(); // this is correct we can call any smarphone as a phone
(Phone obj ) is reference of parent class and new SmartPhone() is object of child class and this can be done but
SmartPhone obj = new Phone(); //this is incorrect because we can't say any phone as smartphone
as (SmartPhone obj ) is reference of child class and new Phone() is object of parent class and this will throw an error
}
}