ProgrammingHardTCS NQT Digital · Accenture Advanced

What is printed?

What is printed?
class A {
    private void f() { System.out.print("A"); }
    void g() { f(); }
}
class B extends A { void f() { System.out.print("B"); } }
// main: new B().g();
  1. A. B
  2. B. AB
  3. C. A
  4. D. Compile error

Correct answer: C. A

Private methods are not inherited, so B.f does not override A.f; g() inside A calls A's private f.

More OOP with C++ and Java questions

Exams that ask this topic