What is the result?

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?

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



Leave a Reply 12

Your email address will not be published. Required fields are marked *


Francesco

Francesco

Compilation fails because in the Horse class the method ride should be declared public.

ses

ses

C, compilation fails.

String ride() should be public
—————
class Icelandic extends Horse {
String ride() { return “tolting “; }
}

ses

ses

and here public too!!
———
class Horse implements Rideable {
String ride() { return “cantering “; }
}

Ray

Ray

The correct is C.
All interface method are public, so the class implements it should be public also.

Eloi

Eloi

The correct answer is C, compilation fails

Ali

Ali

C – default in interface is public and default in class in default (package-protected)

Rad

Rad

Is there anyone to correct the answer of the question?

Matt

Matt

Even if both methods would be public, the shown answer is not correct.

Should be: tolting cantering tolting

DonDorian

DonDorian

I agree