ProgrammingHardIBM · Deloitte pattern
Which statement is correct?
Which statement is correct?
class Base {
final void process() { System.out.print("Base.process"); }
}
class Sub extends Base {
void process() { System.out.print("Sub.process"); }
}- A. It compiles; Sub.process() overrides Base.process()
- B. It fails to compile: a final method cannot be overridden by a subclass
- C. It compiles only if Sub.process() also uses final
- D. It compiles, and calling process() on a Sub instance prints 'Sub.process'
Correct answer: B. It fails to compile: a final method cannot be overridden by a subclass
Marking a method final explicitly forbids any subclass from overriding it, guaranteeing its implementation and dispatch target can never change. Attempting to redeclare process() with the same signature in Sub is a compile-time error.
More OOP with C++ and Java questions
- What is printed? java class Outer { private int secret = 42; class Inner { int reveal() { return secret; } } } public class Main { public…
- Which statement about overload resolution with varargs is correct? java class A { void f(int a, int b) { System.out.print("fixed"); } void…
- Which statement is correct? java interface Shape { double area(); } abstract class AbstractShape implements Shape { public abstract double…
- What is printed? java class Stack<T> { private List<T> items = new ArrayList<>(); void push(T item) { items.add(item); } T pop() { return…
- Which statement is correct? java class Event { protected void log() { System.out.print("Event.log"); } } class ClickEvent extends Event {…
- What is printed? java class Base { public Base() { System.out.print("Base "); } public Base(Base other) { System.out.print("BaseCopy "); }…