Тёмный
No video :(

#59 Upcasting and Downcasting in Java 

Telusko
Подписаться 2,4 млн
Просмотров 98 тыс.
50% 1

Check out our courses:
Enterprise Java Spring Microservices: go.telusko.com...
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : bit.ly/java-sp...
Coupon: TELUSKO20 (20% Discount)
Udemy Courses:
Java:- bit.ly/JavaUde...
Spring:- bit.ly/SpringU...
Java For Programmers:- bit.ly/javaPro...
For More Queries WhatsApp or Call on : +919008963671
website : courses.telusk...
In this lecture we are discussing about:
what is typecasting ?
typecasting is way to converting one primitive data type into other data type using implicit or explicit type
conversion.
double d=4.5;
int i=(int)d; //explicit typecasting
int i1=5;
double d1=i1; //implicit type conversion
When we are talking about non -primitive data type we have concept of upcasting and downcasting
condition for upcasting and downcasting:
-- for that two class should have some parents child relationship
-- if non-primitive data type have no any relationship so, upcasting and downcasting is not possible.
class A{
public void show1(){
System.out.println("In show A)
}
}
class B extends A{
public void show2(){
System.out.println("In show B");
}
}
class Demo{
public static void main(String []args){
A obj =(A) new B(); //upcasting
obj.show1();
A obj1 =new B(); // upcasting
// obj1.show2(); // you get error and you are not able to call show2() because with parennts reference specialised method of child is not visible
here we downcastig to use show2() method
((B)obj).show2(); //downcasting
}
}
Note:
the object can also be typecasted like the datatypes. Parent and Child objects are two types of objects.
So, there are two types of typecasting possible for an object, i.e., Parent to Child and Child to Parent or can say Upcasting and Downcasting.
Github repo : github.com/nav...
Java:- bit.ly/JavaUde...
Spring:- bit.ly/SpringU...
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

Опубликовано:

 

5 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 53   
@imdadhv
@imdadhv Год назад
the parent joke 😂
@savage_001
@savage_001 20 дней назад
😂😂😂
@anshulnegi1822
@anshulnegi1822 12 дней назад
@@savage_001 orphans dosen't aggree bwahahaha
@brawlstarsac3574
@brawlstarsac3574 Год назад
what is the use of this casting? we can directly assign their respective objects, right?
@jan5504
@jan5504 Год назад
Thank you many days been trying to understand this but failed. you explained it to me with very simple yet effective.
@uday2159
@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 ☹️?
@satyanaveenvempati
@satyanaveenvempati Год назад
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 Год назад
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 Год назад
Yes. This is my doubt as well.
@dragnar4743
@dragnar4743 Год назад
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??
@vkvinay6394
@vkvinay6394 Год назад
But it's Showing Exception in type casting . What can do now?
@akashdikshit864
@akashdikshit864 2 месяца назад
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 4 месяца назад
mann!!!! ammmm a fannnnn!!!!! great job in explaining....
@saranyajothi9691
@saranyajothi9691 Год назад
It’s really such a amazing video
@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);
@aninditadas7348
@aninditadas7348 10 месяцев назад
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
@nikitagupta3075
@nikitagupta3075 Год назад
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 Год назад
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 Год назад
​@@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 11 месяцев назад
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
@rohitgade14
@rohitgade14 5 месяцев назад
if we accsing the propety of super class by creating the object of subclass then why we need why we use upcasting
@shiva_lyt
@shiva_lyt 9 месяцев назад
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
@user-wq7ye8ou4c
@user-wq7ye8ou4c 6 месяцев назад
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
@JethalalGada-od9ue
@JethalalGada-od9ue 10 месяцев назад
Hey @Telusko Can you please explain, In upcasting why we can't access methods in class B that are not overridden.
@eiadashraf6086
@eiadashraf6086 10 месяцев назад
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 10 месяцев назад
@@eiadashraf6086 thanks man got it now
@thesea3018
@thesea3018 Год назад
Thanks you man
@sakkuniyash1793
@sakkuniyash1793 Год назад
Sir if the methods names are same in upcasting it invokes child class method ..why??
@BombayAbhishekVlogs
@BombayAbhishekVlogs 10 месяцев назад
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.
@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
@fakename619
@fakename619 4 месяца назад
The parent joke went dark 💀
@Alpha_abi_07
@Alpha_abi_07 3 месяца назад
It's showing error
@user-ow9sg9pj6r
@user-ow9sg9pj6r 11 месяцев назад
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 10 месяцев назад
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)
@user-hf5vp4hg2i
@user-hf5vp4hg2i 3 месяца назад
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 Месяц назад
I got output as: Mobile{model='Apple', price=100000} Mobile{model='Apple', price=100000} false false
@shivi2467
@shivi2467 Год назад
Thank you so much sir❤❤
@rippleyes
@rippleyes 6 месяцев назад
then i found my mentor..
@yuvanshetty6873
@yuvanshetty6873 Месяц назад
Sir 🫡
@promaxmemes1212
@promaxmemes1212 4 месяца назад
its amazing but Videos are not in proper order i guess
@farhodbekxamidov2013
@farhodbekxamidov2013 8 месяцев назад
"A" has no idea😂
@androidgameunboxing3318
@androidgameunboxing3318 Год назад
Code love with Alien Head 👾.
@lavudianjali3967
@lavudianjali3967 7 месяцев назад
That is not the scene in real world, your parents know you😂🤣
@atulpatil-mg7hl
@atulpatil-mg7hl Год назад
Aur confuse kar Diya Bhai tune ,
@souravkarjole7587
@souravkarjole7587 Год назад
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.
@gujjushanmukhasaipavankuma8983
god gifted child.
@souravkarjole7587
@souravkarjole7587 Год назад
@@gujjushanmukhasaipavankuma8983 complementing me or being sarcastic😂
@vkvinay6394
@vkvinay6394 Год назад
But it's Showing Exception in type casting . What can do now?
@user-wq7ye8ou4c
@user-wq7ye8ou4c 6 месяцев назад
If you use Child c= (Child)new Parent(); It will show Runtime exception. Dont use Casting () with "new" keyword
Далее
#60 Wrapper Class in Java
8:08
Просмотров 173 тыс.
Upcasting and Downcasting in Java - Full Tutorial
10:22
#58 Object Class equals toString hashcode in Java
12:00
Java Builder Pattern Explained in 3 Minutes
3:19
Просмотров 13 тыс.
#61 Abstract Keyword in Java
12:09
Просмотров 146 тыс.
#56 Dynamic Method Dispatch in Java
8:04
Просмотров 110 тыс.
Learn Java in 14 Minutes (seriously)
14:00
Просмотров 4,7 млн