ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
class Base {
final void secure() { System.out.print("Base.secure"); }
}
class Derived extends Base {
// void secure() { System.out.print("Derived.secure"); } // line X
}What happens if line X is uncommented?- A. It compiles: Derived.secure overrides Base.secure
- B. Compilation error: cannot override the final method secure() in Base
- C. It compiles: both methods coexist as overloads
- D. It compiles, but Derived.secure is unreachable
Correct answer: B. Compilation error: cannot override the final method secure() in Base
A method marked `final` can never be overridden by any subclass; Derived attempting to redeclare secure() with the identical signature is a direct override attempt, which javac rejects with a compile-time error.
More OOP with C++ and Java questions
- What is printed? cpp class Base { public: virtual void f() final { cout << "Base::f "; } }; class Derived : public Base { public: // void…
- What is printed? cpp struct Base { Base() { cout << "Base() "; } }; struct Mixin1 { Mixin1() { cout << "Mixin1() "; } }; struct Mixin2 {…
- What is printed? java enum Operation { ADD { public int apply(int a, int b) { return a + b; } }, MULTIPLY { public int apply(int a, int b)…
- What is printed? cpp class Shape { public: Shape& operator=(const Shape&) = delete; Shape() = default; }; int main() { Shape a, b; a = b; }
- What is printed? java class Parent { protected int score = 50; int getScore() { return score; } } class Child extends Parent { Child() {…
- What is printed? java class Base { void greet() { System.out.print("Base "); helper(); } private void helper() {…