#59 Upcasting and Downcasting in Java

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ธ.ค. 2024

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

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

    Thank you many days been trying to understand this but failed. you explained it to me with very simple yet effective.

  • @SRUTHIS-w1j
    @SRUTHIS-w1j หลายเดือนก่อน

    Thank you so much. Many days I was trying to understand but couldn't get. You explained really well and very simple. I must say your teaching style is perfect.

  • @imdadhv
    @imdadhv ปีที่แล้ว +49

    the parent joke 😂

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

      😂😂😂

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

      @@savage_001 orphans dosen't aggree bwahahaha

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

      hahahaha I couldn't stop laughing

  • @brawlstarsac3574
    @brawlstarsac3574 ปีที่แล้ว +9

    what is the use of this casting? we can directly assign their respective objects, right?

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

    yeah, this concept is good and always used to confuse me. But, still..... I don't know the purpose of this, like we can access all the child methods and parent methods with normal inheritance. So, why we need upcasting??

  • @akashdikshit864
    @akashdikshit864 5 หลายเดือนก่อน +1

    sir we can access all the members of parent and child by using child object so what is the use of down-casting

  • @Muhammad_Harmain
    @Muhammad_Harmain 7 หลายเดือนก่อน

    mann!!!! ammmm a fannnnn!!!!! great job in explaining....

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

    It’s really such a amazing video

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

    In inheritance we create an object to sub class then we access super class and sub class methods so why we go for down casting

    • @KevalSiddhiKiAur
      @KevalSiddhiKiAur 8 หลายเดือนก่อน

      Sometimes methods are having different access modifiers so it may prevent access by direct objects. By typecasting, u can use any method without making new objects

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

    What is the use of A obj = new B()? We can go in either way? Also what is the use of upcasting and downcasting? Please if somebody can expalin

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

      If you ask for 'use' in particular, then we can only come across the actual use of these things while making projects or industry level projects. So, therefore you should just know that these things exist. So when you actually come across certain situation where you have to use this concept, you can actually make use of downcasting and upcasting to use the same reference variable for calling out multiple methods, keeping inheritance in mind instead of just writing a new reference variable with the new object for every class just to call out their methods again and again...and I bet you are even more confused now after reading this : ) Try watching the video again and then read this, maybe you'll make some sense this time. I'm not mocking you at all, just genuinely speaking : )

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

    Hello Greetings, I have a question here: In the line
    A obj = new B()
    what is the point of referring obj to B class when obj can not be able to call B's method. We can simply do as A obj = new A()

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

      There is no point but if there is situation that while creating a object of class A you want to initialize the B also (e.g. parametrized constructor of B) then you can use this way.

    • @uday2159
      @uday2159 ปีที่แล้ว +4

      ​@@anshuranjan1102 I have one doubt please explain anybody
      In dynamic method dispatch we can call the child method using child object with parent reference BUT why we can't call in this video lecture
      Is it work when methods are same in heritance classes and not work when they are different methods
      ☹️?

    • @287_shaikhmustafa7
      @287_shaikhmustafa7 ปีที่แล้ว

      @@uday2159 it only works with method overriding

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

      plz see carefully in dynamic method dispatch video when we called the child method it is overriiden it also present in parent class and child class, so if method is overridden and we create child object with parent reference then with the help of that child object we can call the overriiden method present in child class but not any other method present in the child class , in this video both show1() and show2() are different. ( if you unable to understand plz watch cwh dynamic method dispatch video your all doubts will be clear)
      ) @@uday2159

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

      ​@@uday2159 your doubt needs to be highlighted

  • @uday2159
    @uday2159 ปีที่แล้ว +6

    I have one doubt please explain anybody
    In dynamic method dispatch we can call the child method using child object with parent reference BUT why we can't call in this video lecture
    Is it work when methods are same in heritance classes and not work when they are different methods
    ☹️?

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

      A method can be overridden if the method name, parameter types and the return type are all the same. As you can see, the method name itself is different here, the overriding will not work and so the object has no idea about the method in sub class.

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

      I'm speaking for JDK 17 above, as before JDK 17, it is different and it is corrected in JDK 17.

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

      @@satyanaveenvempati thanks bro, one more doubt
      Is Overriding belongs to run time polymorphism without using dynamic method dispatch
      I.e., normal overriding create type with same object type

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

      @@uday2159 Sorry, I didn't get your question. Please try to frame it in a different way.

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

      Yes. This is my doubt as well.

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

    But it's Showing Exception in type casting . What can do now?

    • @KevalSiddhiKiAur
      @KevalSiddhiKiAur 8 หลายเดือนก่อน

      If you use
      Child c= (Child)new Parent();
      It will show Runtime exception.
      Dont use Casting () with "new" keyword

  • @haseenabegum-u9h
    @haseenabegum-u9h ปีที่แล้ว +1

    Still confused that why we use the upcasting and downcasting when we can call the methods of superclass and subclass by the same subclass in inheritance,then yyyyyyyyyyeeeeeeeee upcasting and downcasting????????😢😮😮😮😢😢😢😢

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

      correct me if im wrong. i think becaue its more convenient to use the base class as the type. example if you have a big program with a large heirarchy (lots of subclasses) you would have to change the type to the last child class you made so you can also access the methods of the other classes like. its more practical to use the base class as a reference and then downcast when necessary like to avoid duplication and you wont need to create new instances when u add new subclasses (think of multilevel inheritance)

  • @JethalalGada-od9ue
    @JethalalGada-od9ue ปีที่แล้ว

    Hey @Telusko
    Can you please explain, In upcasting why we can't access methods in class B that are not overridden.

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

      The explanation is simple; when you create a reference variable of type A it allocates space in the memory for the contents of the aforementioned class A so it can't contain extra methods in any child class.
      side note if you have overridden a method from A in B what will be called is the one in B even though the reference type is A and this is called boxing if I recall correctly

    • @JethalalGada-od9ue
      @JethalalGada-od9ue ปีที่แล้ว

      @@eiadashraf6086 thanks man got it now

  • @fakename619
    @fakename619 7 หลายเดือนก่อน +3

    The parent joke went dark 💀

  • @rohitgade14
    @rohitgade14 8 หลายเดือนก่อน

    if we accsing the propety of super class by creating the object of subclass then why we need why we use upcasting

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

    Sir if the methods names are same in upcasting it invokes child class method ..why??

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

      Yes bcoz the first priority is that they will give the same class if the method is present then it will invoke otherwise it will search the method in the parent class.

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

    Thanks you man

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

    I have a question , will type casting of StringBuffer/String Builder to/from "String" work.? I am getting error for the same
    String temp = "MALAYALAN";
    StringBuffer sb= new StringBuffer();
    sb = (String)temp;
    String mal = "MALAYALAM2";
    StringBuilder bd = new StringBuilder();
    bd = (StringBuilder)mal;
    But I can append the string to a String Builder.
    bd = bd.append(mal);

    • @Deekshith-tk3iy
      @Deekshith-tk3iy หลายเดือนก่อน

      To convert buffer to string we have to use tostring method

  • @kaibalyasahoo8452
    @kaibalyasahoo8452 2 วันที่ผ่านมา

    Why java is not real in parent condition

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

    I thought from the dynamic method dispatch that since an object of B is being created then Bs methods should be available. Not the methods of the reference variable

  • @rippleyes
    @rippleyes 8 หลายเดือนก่อน

    then i found my mentor..

  • @DevangPatel-b6u
    @DevangPatel-b6u 6 หลายเดือนก่อน

    can someone explain this pls?
    public static void main(String[] args) {
    Mobile mb1 = new Mobile();
    mb1.model = "Apple";
    mb1.price = 100000;
    Mobile mb2 = new Mobile();
    mb2.model = "Apple";
    mb2.price = 100000;
    System.out.println(mb1);
    System.out.println(mb2);
    System.out.println(mb1 == mb2);
    System.out.println(mb1.equals(mb2));
    }
    output:
    Mobile [model=Apple, price=100000]
    Mobile [model=Apple, price=100000]
    false
    true

    • @Abhishek-kt7tk
      @Abhishek-kt7tk 3 หลายเดือนก่อน

      I got output as:
      Mobile{model='Apple', price=100000}
      Mobile{model='Apple', price=100000}
      false
      false

    • @Deekshith-tk3iy
      @Deekshith-tk3iy หลายเดือนก่อน

      ​because while comparing objects it compare based on hashcode so you got false
      Equals method is different from string and object

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

    Thank you so much sir❤❤

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

    Just tell me am i Correct ✅or Wrong ❌(Any one with prior knowledge on the above topic). It would be a GREAT HELP !!🙇‍♂🙇‍♂
    If we say this Gift gift = new Pen(); not only we can leverage the use of polymorphism (if subclass has any overriden methods) but also we will create only one object in the heap, like if further if we say want to access Pen class methods we can just say Pen pen = (Pen) gift; this line can be used to access both superclass methods as well as its own method. But my point is that here we would not have to create a new object in heap instead we can just downcast. So if we say like Gift gift = new Gift() and than we want to use Pen class methods we would need to create object again in heap like ( Pen pen = new Pen() ). So basically instead of creating 2 objects in heap we can just create one object and Downcast it later. This not only helps to write an efficient code but also can leverage the use of Polymorphism with well maintained code.

  • @promaxmemes1212
    @promaxmemes1212 7 หลายเดือนก่อน

    its amazing but Videos are not in proper order i guess

  • @Alpha_abi_07
    @Alpha_abi_07 6 หลายเดือนก่อน

    It's showing error

  • @farhodbekxamidov2013
    @farhodbekxamidov2013 10 หลายเดือนก่อน +1

    "A" has no idea😂

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

    That is not the scene in real world, your parents know you😂🤣

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

    Sir 🫡

  • @atulpatil-mg7hl
    @atulpatil-mg7hl ปีที่แล้ว

    Aur confuse kar Diya Bhai tune ,

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

    Code love with Alien Head 👾.

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

    explanation is not good

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

    But it's Showing Exception in type casting . What can do now?