ProgrammingVery hardGATE CS · TCS NQT Prime · Product Company SDE
A base class method is declared `final` in Java. What does this guarantee about calls to it through a base-class reference, regardless of the object's actual class?
- A. It still uses dynamic dispatch, exactly as if it were not final
- B. It cannot be overridden by any subclass, so the call always statically resolves to that exact method body, with no possibility of dynamic dispatch to a different implementation
- C. Final methods can only be called from within the same package
- D. Declaring a method final has no effect on binding at all
Correct answer: B. It cannot be overridden by any subclass, so the call always statically resolves to that exact method body, with no possibility of dynamic dispatch to a different implementation
A final method cannot be overridden anywhere in the hierarchy, so there is only one possible implementation to call; the JVM (and often the JIT) can bind it statically with no virtual dispatch needed.
More OOP with C++ and Java questions
- cpp struct S { int a, b; S(int x) : b(x), a(b) { } }; int main() { S s(5); cout << s.a << " " << s.b; } Members a and b are declared in…
- What does List<? super Integer> allow, in contrast to List<? extends Integer>?
- java try { throw new ArithmeticException("x"); } catch (RuntimeException e) { System.out.print("R"); } catch (ArithmeticException e) {…
- Which OOP principle bundles data with the methods that act on it and hides the internal state behind an interface?
- In Java, which keyword makes one class inherit from another class?
- What is the return type of a C++ constructor?