Here is my code:-
ArrayList<animal> myanimals = new ArrayList<animal>();
dog adog = new dog();
myanimals.add(adog);
System.out.println("" + myanimals.get(0).getClass());
dog newdog = myanimals.get(0);
I have created an ArrayList of animals(superclass) as myanimals and stored a dog(subclass) as the first element. Then myanimals.get(0) returns a dog type object. When this dog type object is referred by a dog type reference in the statement dog newdog = myanimals.get(0), it shows an error saying :
Type mismatch, cannot convert from animal to dog.
Why does this happen?