Quick revision
The short answer
It prints parent C. Static methods are not overridden: Child.who() hides Parent.who(), and the call p.who() is bound at compile time to the declared type of p, which is Parent. The instance method name() is overridden and dispatched at run time on the actual object, a Child. Fields behave like static methods: a field with the same name in Child hides Parent's field, and which one we read depends on the declared type.
What to remember
In this answer, we saw that the program prints parent C. The static who() is hidden, not overridden, so javac binds p.who() to the declared type Parent with invokestatic and the object is never consulted. The instance method name() is overridden and dispatched at run time with invokevirtual, so the Child version runs. Calling statics through instances is legal but misleading, @Override on a static method is a compile error, and fields of the same name are hidden in the same way as static methods.
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 parent C. Static methods are not overridden: Child.who() hides Parent.who(), and the call p.who() is bound at compile time to the declared type of p, which is Parent. The instance method name() is overridden and dispatched at run time on the actual object, a Child. Fields behave like static methods: a field with the same name in Child hides Parent's field, and which one we read depends on the declared type.
Why this question is asked
This short program is a favourite interview puzzle because it separates two ideas people often blur: overriding, which is chosen at run time from the object, and hiding, which is chosen at compile time from the declared type. The same mistake causes real bugs, for example when a utility class's static method is called through a subclass and the wrong version runs. In this answer, we will recap overriding and hiding, run the program and read its output, explain why each part prints what it does by looking at the bytecode calls, show how the compiler helps us avoid the mistake, and answer the follow-up about fields.
1A Quick Recap of Overriding and Hiding
Overriding applies to instance methods. When a subclass declares an instance method with the same signature as one in its parent, the subclass version replaces it for objects of the subclass, and the JVM picks the version at run time by looking at the actual class of the object. This is dynamic dispatch, and it is how polymorphism works in Java.
Hiding applies to static methods and to fields. A static method belongs to the class, not to any object. If a subclass declares a static method with the same signature as a static method in its parent, the two methods simply coexist, and the subclass one hides the parent one when referred to through the subclass name. Nothing is decided at run time; the compiler chooses which method to call from the type it sees in the source.
2Running the Program
Here is the code from the question, laid out so we can read it.
| Call | Kind | Decided by | Result |
|---|---|---|---|
p.who() | static, hidden | Declared type Parent, at compile time | parent |
p.name() | instance, overridden | Actual object Child, at run time | C |
Child.who() | static | The class named in the call | child |
| ((Parent) null).who() | static | Declared type, no object needed | parent, no exception |
The two halves of the output come from the two different rules. The object is a Child, which is why name() returns C. But p is declared as Parent, which is why who() returns parent.
3Why p.who() Prints parent
When javac compiles p.who(), it sees that who is static. A static call does not need an object, so the compiler ignores the value in p and uses only its declared type, Parent. It emits an invokestatic instruction that names Parent.who directly. At run time, the JVM executes exactly that method, without ever looking at what object p refers to.
We can prove that the object is not used at all. If p were null, p.who() would still print parent without a NullPointerException, because no object is dereferenced. That surprises many people and shows clearly that static calls are bound to types, not to objects.
4Why p.name() Prints C
For p.name(), javac emits an invokevirtual instruction that names Parent.name, because that is what it can see. But invokevirtual means 'find the method with this signature starting from the actual class of the object'. At run time the JVM looks at the object, finds that it is a Child, and calls Child.name(). So the source looks the same for both calls, but the bytecode, and therefore the behaviour, is different.
5Writing It Clearly and Letting the Compiler Help
Calling a static method through an instance variable compiles, but it misleads the reader into thinking polymorphism applies. javac reports it with -Xlint:static, and IDEs and Sonar flag it. The clear form is to call it on the class: Parent.who() or Child.who(), so the code says what actually happens.
The @Override annotation also catches this mistake. If we put @Override on Child.who(), compilation fails with an error that the method does not override a method from its supertype. That is useful: whenever we believe we are overriding, adding @Override makes the compiler confirm it. Java also forbids mixing the two: an instance method cannot override a static one, and a static method cannot hide an instance one; both are compile errors.
6The Follow-Up: Fields With the Same Name
Fields follow the same rule as static methods. They are never polymorphic. If Parent and Child both declare a field called label, a Child object contains two separate fields, and the compiler chooses which one to read from the declared type of the expression.
Methods are the polymorphic way to expose state. If both classes had a label() method instead, p.label() would return the Child's value. This is one reason fields are usually private and accessed through methods.
7Follow-up questions
What happens with fields of the same name in Parent and Child?
The child field hides the parent field. The object then has two fields, and the declared type of the reference decides which one an expression reads: through a Parent reference we see Parent's field, through a Child reference we see Child's. Inside Child, super.label reaches the parent's one. Fields are never dispatched at run time.
Can a static method be inherited at all?
Yes, static methods are inherited in the sense that Child.who() would compile and call Parent.who() if Child did not declare its own. But inherited static methods are still bound at compile time, so declaring one with the same signature in the subclass hides rather than overrides it.
Is there any way to get polymorphic behaviour from static methods?
Not directly. If we need behaviour that varies by type, we should use instance methods, or pass a strategy object or factory. Some code uses reflection to call a static method on a Class object, but that is fragile and usually a sign the design should use instances.
8Conclusion
In this answer, we saw that the program prints parent C. The static who() is hidden, not overridden, so javac binds p.who() to the declared type Parent with invokestatic and the object is never consulted. The instance method name() is overridden and dispatched at run time with invokevirtual, so the Child version runs. Calling statics through instances is legal but misleading, @Override on a static method is a compile error, and fields of the same name are hidden in the same way as static methods.
Open with
It prints parent C. The reason is that the two methods follow different rules. name is an instance method, so Child's version overrides Parent's. javac emits invokevirtual, and at run time the JVM looks at the actual object, which is a Child, and runs Child.name, giving C. who is static, and static methods aren't overridden, they're hidden.
Then, if they want more
A static call doesn't need an object, so the compiler only uses the declared type of p, which is Parent, and emits invokestatic Parent.who. The object is never consulted, which you can see because calling it through a null Parent variable still prints parent without a NullPointerException.
Calling a static method through an instance compiles, but it's misleading, so javac with lint, IDEs and Sonar warn about it, and the right way to write it is Parent.who or Child.who. And if I put @Override on Child.who, the compiler rejects it because it doesn't override anything, which is a good habit for catching this.
For the follow-up about fields: fields behave like static methods. A field with the same name in Child hides Parent's, the object holds two separate fields, and which one you read depends on the declared type of the reference. That's one reason to keep fields private and expose state through methods, which are polymorphic.
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, and why?": https://scrivio-interview-library.netlify.app/java/oop-principles/6372d40370/