Given:
and
Which two method definitions at line n1 in the Bar class compile? (Choose two.)
Given:
and
Which two method definitions at line n1 in the Bar class compile? (Choose two.)
To determine which methods compile, we need to consider Java's rules for method overriding and overloading. Option B defines a method in Bar with the same signature as the one in Foo, which means it properly overrides the method from Foo. Method overriding requires the exact same return type and parameter types. Option C defines a method with the same return type but different parameter types (TreeSet<String> instead of Set<CharSequence>), which is valid as overloading (since it's considered a different method due to the parameters). Thus, both B and C compile successfully.
B ✅ -> overrides: Same method signature including return type C ✅ -> overloads: Same method signature including return type, overloads with a subclass as method parameter
the answer is B and C, B is overriding and C is overloading
BC, not F, because returns "attempting to use incompatible return type"
Tested: B and C.
This is a question about method overriding in Java. The method foo in the Bar class must have the same signature as the foo method in the Foo class to override it. This means that the return type and the parameter types must be the same. Therefore, the correct answers are B and D. Both of these options have a return type of List<Integer> and a parameter type of Set<CharSequence>, which matches the signature of the foo method in the Foo class.
D is not overloading, you cannot change the return type
BC is correct
File Foo.java import java.util.List; import java.util.Set; public class Foo { public List<Integer> foo(Set<CharSequence> m) { return null; } } File Bar.java import java.util.List; import java.util.Set; import java.util.TreeSet; public class Bar extends Foo { // line n1. B //public List<Integer> foo(Set<CharSequence> m) { // return null; //} // line n1. C public List<Integer> foo(TreeSet<String> m) { return null; } } Choose B, C.
F is not for returning Incompatible type and E is not for Name-Clash error in Set parameter. So correct answers is BC
BC is correct not CF since F returning incompatible type.
BC is correct