Java OCA OCP Practice Question 1515

Question

Consider the following code:

class Base{ 
   private float f = 1.0f; 
   void m (float f1){ this.f = f1;  } 
} 
class Base2 extends Base{ 
   private float f = 2.0f; 
   //1 
} 

Which of the following options is a valid example of overriding?

Select 2 options

  • A. protected void m (float f1){ this.f = 2*f1; }
  • B. public void m (double f1){ this.f = (float) 2*f1; }
  • C. public void m (float f1){ this.f = 2*f1; }
  • D. private void m (float f1){ this.f = 2*f1; }
  • E. float m (float f1){ this.f = 2*f1; return f;}


Correct Options are  : A C

Note

An overriding method can be made less restrictive than the overridden method.

The restrictiveness of access modifiers is as follows:

private>default>protected>public 
(where private is most restrictive and public is least restrictive). 

Note that there is no modifier named default.

The absence of any access modifiers implies default access.




PreviousNext

Related