What is the result?

Given:

public class Dog {

protected String bark() {return “woof “; }

}

public class Beagle extends Dog {

private String bark() { return “arf “; }

}

public class TestDog {

public static void main(String[] args) {

Dog[] dogs = {new Dog(), new Beagle()};

for(Dog d: dogs)

System.out.print(d.bark());

}

}

What is the result?

Given:

public class Dog {

protected String bark() {return “woof “; }

}

public class Beagle extends Dog {

private String bark() { return “arf “; }

}

public class TestDog {

public static void main(String[] args) {

Dog[] dogs = {new Dog(), new Beagle()};

for(Dog d: dogs)

System.out.print(d.bark());

}

}

What is the result?

A.
woof arf

B.
woof woof

C.
arf arf

D.
A RuntimeException is generated

E.
The code fails to compile



Leave a Reply 14

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


Boris

Boris

‘protected String bark()’ from Dog and ‘private String bark()’ from Beagle which extends Dog won’t be compiled

Jetendra

Jetendra

Code fails to compile.

“attempting to assign weaker access privileges; was protected”

Francesco

Francesco

So correct answer is E

Ray

Ray

Correct answer is E
Cannot reduce the visibility of the inherited method from Dog

rommy rumhil

rommy rumhil

The correct answer is B “woof woof”

private String bark() { return “arf “; } <– because the bark method on beagle is private

Rudi

Rudi

You’re wrong !!!

Attempting to assign weaker access privileges (private < protected)

Correct answer is E

Charles

Charles

Correct answer should be E
Bark() in Beagle cannot override bark() in Dog; attempting to assign wearker access privilege: was proteted.

Jav

Jav

E is the correct one

mohamed salem

mohamed salem

E also all classes are public this is wrong