ProgrammingHardGATE CS · TCS NQT Prime · Product Company SDE
Why can a private method in a Java superclass never be overridden by a subclass, only unintentionally hidden (i.e. always statically bound)?
- A. Private methods are automatically declared final for an unrelated reason
- B. Private methods are not inherited or even visible to a subclass; a same-named method the subclass declares is really an entirely separate, unrelated method
- C. The JVM cannot resolve any private method call at run time
- D. Private methods actually can be properly overridden in Java
Correct answer: B. Private methods are not inherited or even visible to a subclass; a same-named method the subclass declares is really an entirely separate, unrelated method
Because a subclass cannot even see its superclass's private methods, it cannot override them; any identically-named method it defines is simply a distinct method that happens to share a name.
More OOP with C++ and Java questions
- cpp struct A { int x = 1; }; struct B : A {}; struct C : A {}; struct D : B, C {}; int main() { D d; // d.x ? } Without virtual…
- Why did Java and C# choose single inheritance of classes plus multiple inheritance of interfaces, instead of C++-style full multiple…
- Which statement correctly distinguishes an abstract class from an interface in Java (interfaces may have default/static/private methods…
- Within a single derived class's own constructor body, in what order are its direct base class subobjects (assuming no virtual inheritance)…
- Which statement correctly describes the typical cost of exceptions in C++?
- Why can a generic method not simply write new T[10] for a type parameter T in Java?