OCA Java SE 8 Class Design - OCA Mock Question Class Design 1








Question

What is the result of the following code?

     1: public abstract class Person { 
     2:   private void walk() { System.out.println("Person is walking"); } 
     3:   public static void main(String[] args) { 
     4:     Person p = new Employee(); 
     5:     p.walk(); 
     6:   } 
     7: } 
     8: class Employee extends Person { 
     9:   protected void walk() { System.out.println("Employee is walking"); } 
     10: } 
  1. Person is walking
  2. Employee is walking
  3. The code will not compile because of line 4.
  4. The code will not compile because of line 5.
  5. The code will not compile because of line 9.




Answer



A.

Note

The code compiles and runs without issue, so C, D, and E are incorrect.

walk() is marked as private in the parent class Person, which means it may only be hidden, not overridden.

With hidden methods, the specific method used depends on where it is referenced.

Since it is referenced within the Person class, the method declared on line 2 was used, and option A is correct.