Hiding vs Overriding Interview Questions
What is method hiding vs method overriding? — spoken sample answer for Indian interviews.
- 5Questions with answers
- 3Difficulty levels
Questions (5)
Browse beginner, intermediate, and advanced questions with answers — hide them when you want to self-test.
What is method hiding vs method overriding?
Overriding is an instance method with the same signature. The runtime type wins: Super s = new Sub(); s.instance() runs Sub. Hiding is what static methods do, and what C# 'new' methods do. Binding follows the compile-time type: s.staticMethod() still runs Super if the variable is typed Super. I avoid hiding because it surprises readers. If they show me a snippet with static methods and a subclass, I walk through the reference type first, then the object type.
What is method hiding?
A child declares a static method with the same signature, or C# new on an instance method. Calls follow the compile-time type. Overriding uses virtual dispatch. I would not hide by accident.
Why virtual in C# but not always in Java?
Java instance methods are virtual by default. C# needs virtual and override. That is why C# hiding with new is more visible. The dispatch idea is the same. Defaults differ.
Fields are not polymorphic. What does that mean?
If parent and child both have field x, which field you see depends on the reference type. Methods override, fields hide. I would not put important state in hiding fields. I would use methods.
How would you demo hiding versus overriding in two minutes?
Parent p = new Child(); p.method() for an overridden method — Child runs. Same shape with a static or hidden field — Parent's version shows. Two snippets. If C# I add virtual. If Java I add static.
Practice with AI mock interviews
Run OOP mock interviews with AI follow-ups, instant feedback, and analytics on AiLx.
Free to start · No credit card required