Quick revision
The short answer
It prints animal woof. Fields are not polymorphic: a.name is resolved at compile time from the declared type of a, which is Animal, so it reads Animal's name field even though the object is a Dog that has its own name field. Methods are dynamically dispatched, so a.speak() runs Dog's override and returns woof.
What to remember
In this answer, we saw why the program prints animal woof. The field access a.name is fixed at compile time from the declared type Animal, and the Dog object really holds two name fields, one hidden by the other. The call a.speak() is dispatched at run time from the object's class, so Dog's override runs. We avoided the confusion by keeping one private field set through the constructor and reading it through a method, and used @Override so the compiler confirms that a method really overrides.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
It prints animal woof. Fields are not polymorphic: a.name is resolved at compile time from the declared type of a, which is Animal, so it reads Animal's name field even though the object is a Dog that has its own name field. Methods are dynamically dispatched, so a.speak() runs Dog's override and returns woof.
Background to this question
This snippet looks like a trick question, but it tests one of the most important distinctions in Java: what is decided by the compiler from the variable's type, and what is decided at run time from the object's actual class. Getting it wrong leads to real bugs, usually when a subclass 'redefines' a field and the code that reads it sees the old value. In this answer, we will recap declared types and runtime types, run the code, explain why the field and the method behave differently, look at what the Dog object really contains, and finish with how to avoid field hiding and why @Override is worth writing every time.
1A Quick Recap: Declared Type and Runtime Type
In Animal a = new Dog();, the variable a has a declared type, also called its static type, of Animal. That is the type the compiler sees. The object that a refers to has a runtime type of Dog, because that is what new created. The compiler only knows the declared type; the JVM knows the runtime type when the program runs.
Java uses these two types for different things. When we access a field, the compiler picks which field from the declared type, and that choice is fixed in the compiled code. When we call an instance method that can be overridden, the compiler only checks that Animal has such a method, and the JVM picks the implementation from the runtime type at the moment of the call. That second mechanism is called dynamic dispatch.
2Running the Code
Here is the program with the result of each line.
The first print gives animal woof. a.name uses the declared type Animal, so it reads Animal's field, while a.speak() is dispatched to Dog's method. Notice that the same object gives dog when we look at it through a Dog-typed expression: the cast changes the declared type of the expression, and with it the field the compiler picks. The method result is woof every time, because the object is always a Dog.
3Why Fields and Methods Behave Differently
Field access is compiled into an instruction that names the exact field, such as getfield Animal.name. There is no lookup at run time; the JVM goes straight to that slot in the object. This is the reason fields are fast and also the reason they are not polymorphic. Declaring a field with the same name in a subclass does not replace the parent's field. It adds a second field and hides the first one from code that uses the subclass type. This is called field hiding.
A method call is compiled into an instruction such as invokevirtual Animal.speak. The name and descriptor come from the declared type, but the JVM then looks up which class's speak to run, starting from the object's runtime class, Dog, and walking up the hierarchy. Dog has its own speak, so that one runs. This is overriding, and it is exactly what makes polymorphism work.
| Expression | Decided by | When | Result |
|---|---|---|---|
| a.name | declared type Animal | compile time | "animal" |
a.speak() | runtime type Dog | run time | "woof" |
| ((Dog) a).name | declared type Dog (after cast) | compile time | "dog" |
d.speak() | runtime type Dog | run time | "woof" |
4What the Dog Object Actually Contains
It helps to picture memory. A Dog object holds every field declared in Dog and every field inherited from its superclasses. So this Dog has two separate String fields, both called name: one declared by Animal, holding animal, and one declared by Dog, holding dog. Both are initialised when the object is constructed. Neither overwrites the other.
That is why code written in Animal also sees animal. If Animal had a method describe() that returned name, calling it on a Dog would return animal too, because inside Animal the name always means Animal's field. A common real bug looks exactly like this: a subclass declares a field with the same name to 'change' a default, and the superclass logic keeps using its own value.
5How to Avoid Field Hiding
Hiding fields with the same name is legal but confusing, and most style guides and static analysis tools flag it. If a subclass needs a different value, the usual fixes are to pass it to the superclass constructor, or to make the value come from an overridable method, which is polymorphic. Keeping fields private also removes the problem for callers, because then nobody outside the class reads them directly.
Now there is only one name field, the subclass sets it through the constructor, and every reader goes through name(). Notice also @Override on speak. It does not change behaviour, but it makes the compiler check that the method really overrides one in the superclass. If someone renamed Animal.speak to talk, or misspelled the subclass method as speek, the code would fail to compile instead of silently adding a new method that is never called through an Animal.
6Follow-up questions
Are static methods polymorphic?
No. Static methods are resolved from the declared type, just like fields. If Dog declares a static method with the same signature as a static method in Animal, it hides it, and a call through an Animal-typed expression runs Animal's version. Calling static methods through instance variables is legal but misleading, which is why tools warn about it; call them through the class name.
Can we still reach Animal's field from inside Dog?
Yes, with super.name inside Dog, or by casting this to Animal. That it is possible at all shows that both fields really exist in the object. Needing to do it is usually a sign the design should be changed to a single field.
Does the same thing happen with private methods?
Similar, yes. A private method is not inherited for overriding, so a method with the same signature in the subclass is a separate method. Calls made inside the superclass keep going to the private one. Only non-private, non-static, non-final instance methods are dispatched dynamically.
7Conclusion
In this answer, we saw why the program prints animal woof. The field access a.name is fixed at compile time from the declared type Animal, and the Dog object really holds two name fields, one hidden by the other. The call a.speak() is dispatched at run time from the object's class, so Dog's override runs. We avoided the confusion by keeping one private field set through the constructor and reading it through a method, and used @Override so the compiler confirms that a method really overrides.
Open with
It prints animal woof. The reason is that fields and methods are resolved differently in Java. Field access is not polymorphic.
Then, if they want more
The compiler picks the field from the declared type of the expression, and a is declared as Animal, so a.name reads Animal's name field. The Dog object actually contains two separate name fields, one from Animal with the value animal, and one from Dog with the value dog; Dog's field hides Animal's, it does not replace it.
If I cast a to Dog, I would get dog, because the declared type of the expression changes. Methods, on the other hand, are dynamically dispatched: the compiler only checks that Animal has a speak method, and at run time the JVM looks at the actual object, which is a Dog, and runs Dog's override, so speak returns woof.
In real code I avoid hiding fields with the same name because it causes exactly this confusion; I keep one private field, let the subclass set it through the constructor, and read it through a method, which is polymorphic. And I always write @Override, so the compiler tells me if a method does not really override anything.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does this print?": https://scrivio-interview-library.netlify.app/java/oop-principles/96a3d717ac/