Java OCA OCP Practice Question 487

Question

Consider this class:

1. public class Test1 { 
2.   public float aMethod(float a, float b) { 
3.   } 
4. 
5. } 

Which of the following methods would be legal if added (individually) at line 4?

Choose all that apply.

A.  public int aMethod(int a, int b) { } 
B.  public float aMethod(float a, float b) { } 
C.  public float aMethod(float a, float b, int c) throws Exception { } 
D.  public float aMethod(float c, float d) { } 
E.  private float aMethod(int a, int b, int c) { } 


A, C, E.

Note

In each of these answers, the argument list differs from the original, so the method is an overload.

Overloaded methods are effectively independent, and there are no constraints on the accessibility, return type, or exceptions that may be thrown.

B would be a legal overriding method, except that it cannot be defined in the same class as the original method; rather, it must be declared in a subclass.

D is also an override, because the types of its arguments are the same: changing the parameter names is not sufficient to count as overloading.




PreviousNext

Related