Which of the choices below can legally be inserted at the "insert code here" position in the following code?
class Parent
{
public void print(int i)
{
}
}
public class Child extends Parent
{
public static void main(String argv[])
{
}
// insert code here
}
Choices:
A. public void print(int i, byte b) throws Exception {}
B. public void print(int i, long i) throws Exception {}
C. public void print(long i) {}
D. public void print(int i) throws Exception {}
E. public int print(int i)
Correct choices:
A, B, and C
Explanation:
Option D will not compile because it attempts to throw a checked exception that is not declared in the Parent class. Option E will not compile because only the return type is different; the argument list and method name are the same. This is not allowed in both overriding and overloading. Options A, B, and C have different argument lists, so they represent overloading, not overriding. Because they can throw any exceptions, they are legal
No comments:
Post a Comment