ProgrammingHardHCL · Tech Mahindra pattern
What happens when this is compiled?
What happens when this is compiled?
class A {
final void m() { System.out.println("A"); }
}
class B extends A {
void m() { System.out.println("B"); }
}- A. Compiles; B.m overrides A.m
- B. Compile-time error: m() in A cannot be overridden
- C. Runtime error when B.m is invoked
- D. Compiles; B.m is treated as an overload
Correct answer: B. Compile-time error: m() in A cannot be overridden
A method marked final cannot be overridden by any subclass. B's attempt to redeclare m() with the same signature is rejected at compile time.
More OOP with C++ and Java questions
- What happens when this is compiled? java class Picker { static void f(String s) { System.out.print("String"); } static void…
- What is printed? java class A { static void m() { System.out.println("A.static"); } } class B extends A { static void m() {…
- What happens when this is compiled? cpp struct A { virtual void f() final { cout << "A"; } }; struct B : A { void f() override { cout <<…
- What happens when this is compiled? java class A { int m() { return 1; } double m() { return 1.0; } }
- What is printed? cpp void f(int p) { cout << "nonconst "; } void f(const int p) { cout << "const "; } int main() { int x = 5; int p = &x…
- Does this compile? java class Outer { private int x = 5; class Inner { void show() { System.out.println(x); } } }