java - Why can I add an object of a class to a LinkedList of another class? -
i have class one:
public class 1 { private int idone; private string nameone; //getter , setter }
and class extends one:
public class 2 extends 1 { private string morethings; private string example; //getter , setter }
then have linkedlist<one> mylist = new linkedlist<>();
i dont understand why can add two
objects list if specified list going have one
objects linkedlist<one>
. example:
linkedlist<one> mylist = new linkedlist<>(); 2 t1 = new two(); 2 t2 = new two(); mylist.add(t1); mylist.add(t2);
that cool dont know why can that.
consider problem real world example,let have class animal
, having methods running()
,eating()
, drinking()
etc.now class like
class animal { public void running() { //some code } public void eating() { //some code } public void drinking() { //some code } }
let have 2 other classes dog
, cat
, type of animal can extend animal
class. valid
animal animalobject=new dog(); // dog type of animal or animal animalobject=new cat(); //cat type of animal or dog dogobject=new dog(); //nothing special or cat catobject=new cat(); //nothing special
however ,following invalid
dog dogobject=new animal(); or cat catobject=new animal();
because (read quote again.) , give eye this.. :)
Hello Admin,
ReplyDeleteI surmise you decided to seek after your dream or change your calling way a piece if you are up 'til now getting this!!! Scrutinizing another point could be decently trying anyway doubtlessly compensating at the end.
Regards,
Thanks
RITU