ProgrammingHardHCL · Tech Mahindra pattern
Which statement is correct?
Which statement is correct?
interface Shape {
double area();
}
abstract class AbstractShape implements Shape {
public abstract double area();
public String describe() { return "Area: " + area(); }
}
class Circle extends AbstractShape {
double r = 2;
public double area() { return 3.14 * r * r; }
}- A. It fails to compile: AbstractShape must implement area(), not redeclare it abstract
- B. It compiles fine; an abstract class may redeclare an inherited abstract method as abstract (a no-op), deferring the real implementation to a concrete subclass
- C. It compiles only if AbstractShape is declared final
- D. It fails to compile because describe() calls an unimplemented method
Correct answer: B. It compiles fine; an abstract class may redeclare an inherited abstract method as abstract (a no-op), deferring the real implementation to a concrete subclass
Redeclaring an interface method as abstract inside an abstract class is legal and common (sometimes done just to add stricter Javadoc or re-affirm the contract) — it changes nothing structurally since the class is already abstract. Circle, being concrete, must and does supply a real area().
More OOP with C++ and Java questions
- 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 "); }…
- What is printed? cpp struct Base { virtual void run() { step1(); step2(); } virtual void step1() { cout << "B1 "; } virtual void step2() {…
- What is printed? java class Account { protected double balance; Account(double b) { balance = b; } void transfer(Account to, double amt) {…
- What is printed? cpp struct Base { Base() { cout << "Base "; } Base(int x) { cout << "Base(" << x << ") "; } }; struct Derived : Base {…