ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
interface Shape { double area(); }
class Sq implements Shape {
    double area() { return 4; }
}
// in main:
Shape s = new Sq();
System.out.print(s.area());
  1. A. 4.0
  2. B. 4
  3. C. A runtime AbstractMethodError
  4. D. Compile-time error: area() in Sq has weaker access than in Shape

Correct answer: D. Compile-time error: area() in Sq has weaker access than in Shape

Interface methods are implicitly public, and an overriding method cannot reduce visibility. Sq.area() is package-private, so the class does not compile; it must be declared public.

More OOP with C++ and Java questions

Exams that ask this topic