ProgrammingVery hardProduct company OA
What is printed?
What is printed?
class Vehicle {
void start() { System.out.print("Vehicle starts "); }
}
interface Electric {
default void start() { System.out.print("Electric starts "); }
}
class Tesla extends Vehicle implements Electric {
@Override
void start() { super.start(); Electric.super.start(); }
}- A. Compilation error: start() is ambiguous between the class and the interface
- B. Compilation error: cannot call Electric.super.start() from a subclass
- C. Compilation error: default methods cannot be overridden
- D. Compiles fine: a class method always wins over an interface default without needing disambiguation, but explicit qualification is still allowed for clarity here
Correct answer: D. Compiles fine: a class method always wins over an interface default without needing disambiguation, but explicit qualification is still allowed for clarity here
Java's conflict-resolution rule states a concrete class method (even an inherited one from a superclass) always takes priority over an interface's default method, so there's no ambiguity to begin with — Tesla could even omit the override entirely and still get Vehicle::start. The explicit super.start() and Electric.super.start() calls shown here are both legal, deliberate ways to invoke each one specifically.
More OOP with C++ and Java questions
- What is printed? java interface Counter { int next(); private int helper() { return 100; } default int nextPlusHelper() { return next() +…
- What is printed? cpp class Account { double balance = 0; friend void deposit(Account& a, double amt); public: double getBalance() const {…
- What is printed? java abstract class Shape { abstract double area(); } interface Colorable { String color(); } class Circle extends Shape…
- What is printed? java class Base implements Cloneable { int x = 5; @Override public Base clone() throws CloneNotSupportedException {…
- What is printed? java class Node<T extends Comparable<T>> { T value; Node(T v) { value = v; } boolean isGreaterThan(Node<T> other) {…
- What is printed? cpp struct Base { virtual void greet() const { cout << "Base "; } }; struct Derived : Base { void greet() const override…