i´m still learning Java (and this is my first question on SO), so please forgive me if this kind of question is misplaced here or if my wording isn´t correct!
I´m looking into inheritance and polymorphism at the moment and weren´t able to answer the following question of mine, despite googling quite a few.
public class Animal {
public String name = "Bello";
}
public class Cat extends Animal {
public String name = "Kitty";
public static void main(String[] args) {
Cat cat1 = new Cat();
Animal animal1 = cat1;
System.out.println(cat1.name);
System.out.println(animal1.name);
}
}
System.out.println(cat1.name);
prints out Kitty
, but System.out.println(animal1.name);
prints out Bello
So apperently the value of the attribute name gets changed as the instance of the child class is saved as an object of the parent class. Why does the parent class change the value into her own default-value, even it is already set in cat1? Seems there is sth. to know which i wasn´t able to figure out..