Discussion
Which line rests on a mistaken assumption?
public class Sample39
{
static class Base { String who() { return "base"; } }
static class Sub extends Base { String who() { return "sub"; } }
public static void main(String[] args)
{
Base b = new Sub();
assert b.who().equals("base") : b.who();
System.out.println(b.who());
}
}
Answer: C
Instance methods dispatch on the runtime type, so b.who() returns sub even though the variable is declared Base. The assertion assumes the declared type decides, which is only true for static methods and fields. It therefore fails on entirely correct polymorphism.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development