#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
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 39

  • @user-mk6um6rh1j
    @user-mk6um6rh1j 10 หลายเดือนก่อน +32

    Really you deserve a Dhrona Charya award sir. I've learn a looooooooot from you than I had learnt from my college.

    • @BLG120
      @BLG120 5 หลายเดือนก่อน +4

      Dont flex too much. It ll be a loooooooooooot of problem for u in future

    • @thangutabbycat
      @thangutabbycat 3 หลายเดือนก่อน +1

      That's too over bro 😂

  • @MohamedWasim-gh6eh
    @MohamedWasim-gh6eh 6 หลายเดือนก่อน +4

    Sir you have God gifted knowledge no one can explain this clear thank you.

  • @kilvish25
    @kilvish25 หลายเดือนก่อน +2

    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.

  • @vijankaush7308
    @vijankaush7308 ปีที่แล้ว +11

    The best JAVA tutorial series ever

  • @user-ir2vf2nw6d
    @user-ir2vf2nw6d ปีที่แล้ว +6

    easy to understand! you are my saver!!

  • @yeshwanthreddy6014
    @yeshwanthreddy6014 6 หลายเดือนก่อน +2

    Great place to learn or brush up java. Keep doing this great work sir.

  • @vasanthv4233
    @vasanthv4233 ปีที่แล้ว +2

    superb explanation Sir thank you!

  • @janardhanreddyboyalla2941
    @janardhanreddyboyalla2941 ปีที่แล้ว +2

    Great Explanation

  • @raghav9721
    @raghav9721 6 หลายเดือนก่อน +2

    As am a Java Trainee Trainer Telusko videos are 💎

  • @bhabagrahibarik1239
    @bhabagrahibarik1239 3 หลายเดือนก่อน

    The best explaination ever in few minutes. Thanku sir for the best explanation.

  • @sinekad5132
    @sinekad5132 11 หลายเดือนก่อน +1

    Thank you sir❤

  • @onlysubscriptions2152
    @onlysubscriptions2152 6 วันที่ผ่านมา

    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.

  • @PraveenKumar-sk5sd
    @PraveenKumar-sk5sd 5 หลายเดือนก่อน

    Very Clear Understanding For Me...

  • @avichiii
    @avichiii 3 หลายเดือนก่อน

    that was an beautiful explanation. you earned a sub

  • @k.kanimation4436
    @k.kanimation4436 3 หลายเดือนก่อน

    Thanks a lot sir for ez understanding series💖

  • @chakkers
    @chakkers ปีที่แล้ว +3

    Sir will this work if B extends A and C extends B. Is it possible to call show method of class A from C

  • @mackensonreginaldmichel399
    @mackensonreginaldmichel399 ปีที่แล้ว +3

    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?

    • @meenakshiviswanathan8409
      @meenakshiviswanathan8409 ปีที่แล้ว +2

      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)

  • @agariogg172
    @agariogg172 11 หลายเดือนก่อน

    Excellent explanation, I just noticed you look a little bit like Nacho from Better Call Saul :)

  • @sreeramv1579
    @sreeramv1579 17 วันที่ผ่านมา

    Then what is the difference between function overriding and polymorphism sir?

  • @shreedharmakr2523
    @shreedharmakr2523 ปีที่แล้ว +1

    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?

  • @user-sw1mg8ni6l
    @user-sw1mg8ni6l 3 หลายเดือนก่อน

    The example of laptop and computer is best.

  • @funoverflow
    @funoverflow 4 หลายเดือนก่อน

    3:16 this is a heterogeneous object creation right?

  • @user-ke1fj9eo6v
    @user-ke1fj9eo6v 5 หลายเดือนก่อน +2

    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

    • @VarunGowda-ho2rm
      @VarunGowda-ho2rm 2 หลายเดือนก่อน

      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

  • @shaiknagurbasha9657
    @shaiknagurbasha9657 4 หลายเดือนก่อน

    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.....

  • @neerajgarg9096
    @neerajgarg9096 10 หลายเดือนก่อน

    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?

    • @mrigankadhar8204
      @mrigankadhar8204 5 หลายเดือนก่อน

      Yes you are correct. I was looking a comment which had the same query. Finally found one guy !! cheers

  • @gujjushanmukhasaipavankuma8983
    @gujjushanmukhasaipavankuma8983 ปีที่แล้ว

    Runtime polymorphism or Dynamic Method Dispatch

  • @radheambhure2112
    @radheambhure2112 ปีที่แล้ว +1

    Yogiji : Aj se tumhara naam taklusko

  • @ehteerk
    @ehteerk 11 หลายเดือนก่อน

    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

    • @sonujhariya
      @sonujhariya 9 หลายเดือนก่อน +1

      that's exactly how you do.

  • @varuntanwar6746
    @varuntanwar6746 ปีที่แล้ว

    once we have gone through*

  • @rooparoy7775
    @rooparoy7775 ปีที่แล้ว

    Where are u from sir?

  • @karanvirsagar1998
    @karanvirsagar1998 ปีที่แล้ว

    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....

    • @neerajgarg9096
      @neerajgarg9096 10 หลายเดือนก่อน +2

      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
      }
      }