#59 Upcasting and Downcasting in Java

Поделиться
HTML-код
  • Опубликовано: 20 янв 2025

Комментарии • 64

  • @SRUTHIS-w1j
    @SRUTHIS-w1j 3 месяца назад +1

    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.

  • @jan5504
    @jan5504 Год назад +3

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

  • @imdadhv
    @imdadhv 2 года назад +55

    the parent joke 😂

    • @savage_001
      @savage_001 5 месяцев назад

      😂😂😂

    • @anshulnegi1822
      @anshulnegi1822 4 месяца назад

      @@savage_001 orphans dosen't aggree bwahahaha

    • @modelmaestrosolo
      @modelmaestrosolo 3 месяца назад

      hahahaha I couldn't stop laughing

  • @brawlstarsac3574
    @brawlstarsac3574 Год назад +11

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

  • @saranyajothi9691
    @saranyajothi9691 Год назад +2

    It’s really such a amazing video

  • @dragnar4743
    @dragnar4743 Год назад +4

    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 6 месяцев назад +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 9 месяцев назад

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

  • @nikitagupta3075
    @nikitagupta3075 2 года назад +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 2 года назад +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 3 месяца назад

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

  • @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 10 месяцев назад

      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 Год назад +2

    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 3 месяца назад

      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 : )

  • @sinchanam902
    @sinchanam902 26 дней назад

    Hey can anyone explain ….
    It’s showing error while typecasting…
    Why it is showing Can’t typecast from B to A in my vscode

  • @vkvinay6394
    @vkvinay6394 Год назад +1

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

    • @KevalSiddhiKiAur
      @KevalSiddhiKiAur 10 месяцев назад

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

  • @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

  • @ajayghode3602
    @ajayghode3602 29 дней назад

    Thank You

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

  • @nidhinrv317
    @nidhinrv317 4 месяца назад

    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 3 месяца назад

      To convert buffer to string we have to use tostring method

  • @rohitgade14
    @rohitgade14 10 месяцев назад

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

  • @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)

  • @fakename619
    @fakename619 9 месяцев назад +3

    The parent joke went dark 💀

  • @thesea3018
    @thesea3018 Год назад

    Thanks you man

  • @kaibalyasahoo8452
    @kaibalyasahoo8452 Месяц назад

    Why java is not real in parent condition

  • @Alpha_abi_07
    @Alpha_abi_07 8 месяцев назад

    It's showing error

  • @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

  • @shivi2467
    @shivi2467 Год назад

    Thank you so much sir❤❤

  • @DevangPatel-b6u
    @DevangPatel-b6u 7 месяцев назад

    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 5 месяцев назад

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

    • @Deekshith-tk3iy
      @Deekshith-tk3iy 3 месяца назад

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

  • @promaxmemes1212
    @promaxmemes1212 9 месяцев назад

    its amazing but Videos are not in proper order i guess

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

  • @rippleyes
    @rippleyes 10 месяцев назад

    then i found my mentor..

  • @farhodbekxamidov2013
    @farhodbekxamidov2013 Год назад +1

    "A" has no idea😂

  • @lavudianjali3967
    @lavudianjali3967 11 месяцев назад

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

  • @yuvanshetty6873
    @yuvanshetty6873 6 месяцев назад

    Sir 🫡

  • @Chandru056
    @Chandru056 3 месяца назад +1

    explanation is not good

  • @atulpatil-mg7hl
    @atulpatil-mg7hl Год назад

    Aur confuse kar Diya Bhai tune ,

  • @androidgameunboxing3318
    @androidgameunboxing3318 2 года назад

    Code love with Alien Head 👾.

  • @vkvinay6394
    @vkvinay6394 Год назад +3

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