Java OCA OCP Practice Question 2644

Question

Consider the following program:

class BaseClass {
   private void foo() {
      System.out.println("In BaseClass.foo()");
   }/* ww w.  java 2 s . c o  m*/

   void bar() {
      System.out.println("In BaseClass.bar()");
   }

   public static void main(String[] args) {
      BaseClass po = new DerivedClass();
      po.foo(); // BASE_FOO_CALL
      po.bar();
   }
}

class DerivedClass extends BaseClass {
   void foo() {
      System.out.println("In Derived.foo()");
   }

   void bar() {
      System.out.println("In Derived.bar()");
   }
}

Which one of the following options correctly describes the behavior of this program?

a)This program results in a compiler error in the line marked 
  with the comment BASE_FOO_CALL./* ww w.j a  v  a 2 s.c  o  m*/

b)This program prints the following:
  In BaseClass.foo()
  In BaseClass.bar()

c)This program prints the following:
  In BaseClass.foo()
  In Derived.bar()

d)This program prints the following:
  In Derived.foo()
  In Derived.bar()
  


c)

Note

Since a private method is not visible to any other classes, including its derived classes, it cannot be overridden.




PreviousNext

Related