The right answer is the letter E, class C cannot be B because B extends A and C also extends C, there is no inheritance between C and B because neither of the 2 classes extends the other, generating the exception Class Cast Exception on the line 2 where trying to perform the polymorphism of C -> B. I also tested the code, if you want to test it too it is below:
class A {
public void test () {
System.out.println ("A");
}
}
class B extends A {
public void test () {
System.out.println ("B");
}
}
public class C extends A {
public void test () {
System.out.println ("C");
}
public static void main(String[] args) {
A b1 = new A ();
A b2 = new C ();
b1 = (A) b2; //line n1
A b3 = (B) b2; //line n2
b1.test ();
b3.test ();
}
}
E : B is subclass of A, C is subclass of A, we can cast from one to another via implicite and explicite casting
C is not a subclass of B, so we can't cast from C to B and vice versal
Answer is E. It shows the error as " Exception in thread "main" java.lang.ClassCastException: class p1.Acc cannot be cast to class p1.B (p1.Acc and p1.B are in module KK.java of loader 'app')
at KK.java/p1.Acc.main(Acc.java:20)
"
upvoted 1 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.
iSnover
Highly Voted 2 years, 3 months agoMPignaProTech
Most Recent 3 months agoMPignaProTech
3 months ago7df49fb
10 months agosomrita90
1 year, 4 months agoSreeni_A
1 year, 5 months agoIbrahimAlnutayfi
1 year, 6 months agoeilla
2 years, 1 month agocarloswork
2 years, 2 months agokkaayyyy
2 years, 3 months ago