Tested. Answer is D.
To test:
class Bird {
public void fly() {
System.out.print("Fly.");
}
}
class Peacock extends Bird {
public void dance() {
System.out.print("Dance.");
}
}
public class Test {
public static void main(String[] args) {
Bird b = new Peacock (); Peacock p = (Peacock) b;
p.fly();
p.dance();
}
}
The correct answer is the letter B, you need to create an object B and then do a casting for it, making the same be able to access the method of the child class.
You're are wrong. Correct answer is D. Option B is not how downcasting works. Option D is the textbook definition of downcating. With option our object type is peacock and class type of reference variable is also Peacock and peacock is a child class. Child class peacock can access it's method as well as Parent class Bird method.
Correct answer is D. For option B there is ClassCastException as class Bird cannot be casted to class Peacock
upvoted 4 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
MPignaProTech
2 months, 3 weeks agoBelloMio
7 months, 1 week ago7df49fb
9 months, 3 weeks agoDriftKing
1 year, 4 months agoamigo31
2 years, 1 month agocarloswork
2 years, 2 months agoUAK94
2 years, 2 months agoiSnover
2 years, 3 months agoDriftKing
1 year, 4 months agoDriftKing
1 year, 4 months agoalex_au
2 years, 3 months ago