#56 Dynamic Method Dispatch in Java
ฝัง
- เผยแพร่เมื่อ 17 ม.ค. 2023
- Check out our courses:
Enterprise Java Spring Microservices: go.telusko.com/enterpriseJava
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : bit.ly/java-spring-cloud
Coupon: TELUSKO20 (20% Discount)
Udemy Courses:
Java:- bit.ly/JavaUdemyTelusko
Spring:- bit.ly/SpringUdemyTelusko
Java For Programmers:- bit.ly/javaProgrammers
For More Queries WhatsApp or Call on : +919008963671
website : courses.telusko.com/
in this video we are discussing method dispatch
-- in previous lecture we had discussed what is polymorphism
-- in this lecture we are further talking about run time polymorphism
-- suppose we have some class A , B and C
-- class B and C extends A
-- in All three class we have show() method
-- main() method of Demo we create object of A, B and C but we create only reference of A which can hold
object of A, B and C .
class A{
public void show(){
Systeem.out.println("in show A");
}
}
class B extends A{
public void show(){
Systeem.out.println("in show B");
}
}
class C extends A{
public void show(){
Systeem.out.println("in show C");
}
}
public class Demo{
public static void main(String []args){
A obj =new A();
obj.show(); //Output: in show A
obj =new B(); //reference is A (we can use reference of parents) and create object of B and assign to parents reference variable.
obj.show(); //Output: in show B
obj =new B(); //reference is A (we can use reference of parents) and create object of C and assign to parents reference variable.
obj.show(); //Output: in show C
}
}
Note: during compile time we donot which show() method is called from which class.
-- we can know during run time which show method is called this is known as run time polymorphism.
-- all this concept is class dynamic method dispatch
Github repo : github.com/navinreddy20/Javac...
Java:- bit.ly/JavaUdemyTelusko
Spring:- bit.ly/SpringUdemyTelusko
More Learning :
Java :- bit.ly/3x6rr0N
Python :- bit.ly/3GRc7JX
Django :- bit.ly/3MmoJK6
JavaScript :- bit.ly/3tiAlHo
Node JS :- bit.ly/3GT4liq
Rest Api :-bit.ly/3MjhZwt
Servlet :- bit.ly/3Q7eA7k
Spring Framework :- bit.ly/3xi7buh
Design Patterns in Java :- bit.ly/3MocXiq
Docker :- bit.ly/3xjWzLA
Blockchain Tutorial :- bit.ly/3NSbOkc
Corda Tutorial:- bit.ly/3thbUKa
Hyperledger Fabric :- bit.ly/38RZCRB
NoSQL Tutorial :- bit.ly/3aJpRuc
Mysql Tutorial :- bit.ly/3thpr4L
Data Structures using Java :- bit.ly/3MuJa7S
Git Tutorial :- bit.ly/3NXyCPu
Donation:
PayPal Id : navinreddy20
www.telusko.com - วิทยาศาสตร์และเทคโนโลยี
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 😂
Sir you have God gifted knowledge no one can explain this clear thank you.
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.
The best JAVA tutorial series ever
easy to understand! you are my saver!!
Great place to learn or brush up java. Keep doing this great work sir.
superb explanation Sir thank you!
Great Explanation
As am a Java Trainee Trainer Telusko videos are 💎
The best explaination ever in few minutes. Thanku sir for the best explanation.
Thank you sir❤
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.
Very Clear Understanding For Me...
that was an beautiful explanation. you earned a sub
Thanks a lot sir for ez understanding series💖
Sir will this work if B extends A and C extends B. Is it possible to call show method of class A from C
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)
Excellent explanation, I just noticed you look a little bit like Nacho from Better Call Saul :)
Then what is the difference between function overriding and polymorphism sir?
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
The example of laptop and computer is best.
3:16 this is a heterogeneous object creation right?
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?
Yes you are correct. I was looking a comment which had the same query. Finally found one guy !! cheers
Runtime polymorphism or Dynamic Method Dispatch
Yogiji : Aj se tumhara naam taklusko
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.
once we have gone through*
Where are u from sir?
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
}
}