Given:
interface Rideable {
String ride() ;
}
class Horse implements Rideable {
String ride() { return “cantering “; }
}
class Icelandic extends Horse {
String ride() { return “tolting “; }
}
class Test {
public static void main(String[] args) {
Rideable r1 = new Icelandic();
Rideable r2 = new Horse();
Horse h1 = new Icelandic();
System.out.println(r1.ride() + r2.ride() + h1.ride());
}
}
What is the result?
A.
tolting cantering cantering
B.
cantering cantering cantering
C.
compilation fails
D.
an exception is thrown at runtime
Compilation fails because in the Horse class the method ride should be declared public.
C, compilation fails.
String ride() should be public
—————
class Icelandic extends Horse {
String ride() { return “tolting “; }
}
and here public too!!
———
class Horse implements Rideable {
String ride() { return “cantering “; }
}
The correct is C.
All interface method are public, so the class implements it should be public also.
The correct answer is C, compilation fails
C – default in interface is public and default in class in default (package-protected)
C
Is there anyone to correct the answer of the question?
YEs C
Even if both methods would be public, the shown answer is not correct.
Should be: tolting cantering tolting
I agree
c