Output comes as "furry bray". Please explain this.
fields
in java programs are not accessed via dynamic lookup. Instead they are resolved statically while compile time. That's why you are getting furry
for m.name
. Whereas, methods
in java programs are accessed via dynamic lookup. That's why you are getting bray
for m.makeNoise()
.
And how can we access the name variable, the one having "stripes " in
it?
And if you want to access Zebra.name
, you should type cast m
to 'Zebra'.This would look like this:
System.out.println(((Zebra)m).name + m.makeNoise());
UPDATE
The phenomena that is exhibiting here is the result of Fields Hiding rather than variable shadowing.