Open gwajoon opened 3 years ago
Hi, for List< ? extends T >, you cannot add objects to the class because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object you want to add is allowed in that list. The only "guarantee" is that you can read from it and you will get a T or a subclass of T. For example, let Integers and Doubles be a subclass of Numbers. List < ? extends Numbers > foo = new ArrayList< Numbers >(), List < ? extends Numbers > foo = new ArrayList< Integers >(), and List < ? extends Numbers > foo = new ArrayList< Doubles >() are all valid. However, when you want to write to foo, you cannot add an Integer because foo may be pointing to a List < Doubles >, you cannot add a Double because foo may be pointing to a List< Integers >, and you cannot add a Number because foo may be pointing to a List < Numbers >. Hence there is no guarantee of writing to the list
Hi gwajoon, I found this image online, hopefully it helps hehe
Hi, for List< ? extends T >, you cannot add objects to the class because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object you want to add is allowed in that list. The only "guarantee" is that you can read from it and you will get a T or a subclass of T. For example, let Integers and Doubles be a subclass of Numbers. List < ? extends Numbers > foo = new ArrayList< Numbers >(), List < ? extends Numbers > foo = new ArrayList< Integers >(), and List < ? extends Numbers > foo = new ArrayList< Doubles >() are all valid. However, when you want to write to foo, you cannot add an Integer because foo may be pointing to a List < Doubles >, you cannot add a Double because foo may be pointing to a List< Integers >, and you cannot add a Number because foo may be pointing to a List < Numbers >. Hence there is no guarantee of writing to the list
Thanks Keith, I initially had this problem in the first place, thanks for clarifying for me! This comment has been a godsend! xoxo
you're very much welcome! Do hit me up if you have further queries. Keep living it big
Yup I will! Looking forward to interacting with you in the future if I have any more questions!
you're very much welcome! Do hit me up if you have further queries. Keep living it big
Hi, for List< ? extends T >, you cannot add objects to the class because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object you want to add is allowed in that list. The only "guarantee" is that you can read from it and you will get a T or a subclass of T. For example, let Integers and Doubles be a subclass of Numbers. List < ? extends Numbers > foo = new ArrayList< Numbers >(), List < ? extends Numbers > foo = new ArrayList< Integers >(), and List < ? extends Numbers > foo = new ArrayList< Doubles >() are all valid. However, when you want to write to foo, you cannot add an Integer because foo may be pointing to a List < Doubles >, you cannot add a Double because foo may be pointing to a List< Integers >, and you cannot add a Number because foo may be pointing to a List < Numbers >. Hence there is no guarantee of writing to the list
Thanks Keith, I initially had this problem in the first place, thanks for clarifying for me! This comment has been a godsend! xoxo
Hi jettyevy, could u explain it to me more in depth maybe over zoom? still cant really grasp this concept.
Wentinggy, how did you know I was a visual learner? That image helped me understand the concept a lot better at one glance! Keithlimhs, thank you for your enlightenment. I frequently see you enlightening other users here, please keep up the good work and spirit of peer-to-peer learning! You guys are really making the community grow together one step at a time :)
Hi, for List< ? extends T >, you cannot add objects to the class because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object you want to add is allowed in that list. The only "guarantee" is that you can read from it and you will get a T or a subclass of T. For example, let Integers and Doubles be a subclass of Numbers. List < ? extends Numbers > foo = new ArrayList< Numbers >(), List < ? extends Numbers > foo = new ArrayList< Integers >(), and List < ? extends Numbers > foo = new ArrayList< Doubles >() are all valid. However, when you want to write to foo, you cannot add an Integer because foo may be pointing to a List < Doubles >, you cannot add a Double because foo may be pointing to a List< Integers >, and you cannot add a Number because foo may be pointing to a List < Numbers >. Hence there is no guarantee of writing to the list
Thanks Keith, I initially had this problem in the first place, thanks for clarifying for me! This comment has been a godsend! xoxo
Hi jettyevy, could u explain it to me more in depth maybe over zoom? still cant really grasp this concept.
Yup! I will be doing a zoom session with Keithlimhs tonight! I'll pm you our zoom link!
Hi, for List< ? extends T >, you cannot add objects to the class because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object you want to add is allowed in that list. The only "guarantee" is that you can read from it and you will get a T or a subclass of T. For example, let Integers and Doubles be a subclass of Numbers. List < ? extends Numbers > foo = new ArrayList< Numbers >(), List < ? extends Numbers > foo = new ArrayList< Integers >(), and List < ? extends Numbers > foo = new ArrayList< Doubles >() are all valid. However, when you want to write to foo, you cannot add an Integer because foo may be pointing to a List < Doubles >, you cannot add a Double because foo may be pointing to a List< Integers >, and you cannot add a Number because foo may be pointing to a List < Numbers >. Hence there is no guarantee of writing to the list
Thanks Keith, I initially had this problem in the first place, thanks for clarifying for me! This comment has been a godsend! xoxo
Hi jettyevy, could u explain it to me more in depth maybe over zoom? still cant really grasp this concept.
Yup! I will be doing a zoom session with Keithlimhs tonight! I'll pm you our zoom link!
Can hit me up with the zoom link?
Hi, for List< ? extends T >, you cannot add objects to the class because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object you want to add is allowed in that list. The only "guarantee" is that you can read from it and you will get a T or a subclass of T. For example, let Integers and Doubles be a subclass of Numbers. List < ? extends Numbers > foo = new ArrayList< Numbers >(), List < ? extends Numbers > foo = new ArrayList< Integers >(), and List < ? extends Numbers > foo = new ArrayList< Doubles >() are all valid. However, when you want to write to foo, you cannot add an Integer because foo may be pointing to a List < Doubles >, you cannot add a Double because foo may be pointing to a List< Integers >, and you cannot add a Number because foo may be pointing to a List < Numbers >. Hence there is no guarantee of writing to the list
Thanks Keith, I initially had this problem in the first place, thanks for clarifying for me! This comment has been a godsend! xoxo
Hi jettyevy, could u explain it to me more in depth maybe over zoom? still cant really grasp this concept.
Yup! I will be doing a zoom session with Keithlimhs tonight! I'll pm you our zoom link!
Can hit me up with the zoom link?
Hi gwajoon, jettyevy, keithlimhs, it was really nice connecting with all of you guys over zoom since classes took a huge online turn due to covid. Thank you for clarifying my misconceptions and I hope my analogies helped you, for example, i remember Function interface has a SAM called apply from this pneumonic, FIHASCA.
I am rather unclear about the concept of PECS, and was trying to wrap my mind around it after my TA explained it to us during lab. From what I know, <? extends T> is used when you know that the wildcard needs to be a wrapper class that extends the type T. And I remember someone in my lab mentioning that it becomes read-only and you cannot add objects to the class. But I am still confused about the whole idea of it does anybody have a example that can help me visualise this better?
Specific Topics this question is related to (If Any)
Screenshot of the slide: