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.
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??
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
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 : )
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()
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.
@@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 ☹️?
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
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 ☹️?
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 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
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????????😢😮😮😮😢😢😢😢
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)
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
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.
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);
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
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.
Thank you many days been trying to understand this but failed. you explained it to me with very simple yet effective.
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.
the parent joke 😂
😂😂😂
@@savage_001 orphans dosen't aggree bwahahaha
hahahaha I couldn't stop laughing
what is the use of this casting? we can directly assign their respective objects, right?
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??
sir we can access all the members of parent and child by using child object so what is the use of down-casting
mann!!!! ammmm a fannnnn!!!!! great job in explaining....
It’s really such a amazing video
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
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
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
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 : )
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()
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.
@@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
☹️?
@@uday2159 it only works with method overriding
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
@@uday2159 your doubt needs to be highlighted
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
☹️?
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.
I'm speaking for JDK 17 above, as before JDK 17, it is different and it is corrected in JDK 17.
@@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
@@uday2159 Sorry, I didn't get your question. Please try to frame it in a different way.
Yes. This is my doubt as well.
But it's Showing Exception in type casting . What can do now?
If you use
Child c= (Child)new Parent();
It will show Runtime exception.
Dont use Casting () with "new" keyword
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????????😢😮😮😮😢😢😢😢
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)
Hey @Telusko
Can you please explain, In upcasting why we can't access methods in class B that are not overridden.
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
@@eiadashraf6086 thanks man got it now
The parent joke went dark 💀
if we accsing the propety of super class by creating the object of subclass then why we need why we use upcasting
Sir if the methods names are same in upcasting it invokes child class method ..why??
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.
Thanks you man
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);
To convert buffer to string we have to use tostring method
Why java is not real in parent condition
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
then i found my mentor..
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
I got output as:
Mobile{model='Apple', price=100000}
Mobile{model='Apple', price=100000}
false
false
because while comparing objects it compare based on hashcode so you got false
Equals method is different from string and object
Thank you so much sir❤❤
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.
god gifted child.
@@gujjushanmukhasaipavankuma8983 complementing me or being sarcastic😂
its amazing but Videos are not in proper order i guess
It's showing error
"A" has no idea😂
That is not the scene in real world, your parents know you😂🤣
Sir 🫡
Aur confuse kar Diya Bhai tune ,
Code love with Alien Head 👾.
explanation is not good
But it's Showing Exception in type casting . What can do now?