The letter D is correct, a ClassCastException occurs, because B2 is an object of type C and class C has no inheritance from class B, so it cannot overload.
class A {
public void test() {
System.out.println("A");
}
}
class B extends A{
public void test() {
System.out.println("B");
}
}
public class C extends B {
public void test() {
System.out.println("C");
}
public static void main(String[] args) throws InterruptedException {
A b1= new A();
A b2 = new C();
A b3 = (B) b2;
b1 = (A) b2;
b1.test();
b3.test();
}
}
Answer: C
class A {
public void test() {
System.out.println("A");
}
}
class B extends A{
public void test() {
System.out.println("B");
}
}
public class C extends B {
public void test() {
System.out.println("C");
}
public static void main(String[] args) throws InterruptedException {
A b1= new A();
A b2 = new C();
A b3 = (B) b2;
b1 = (A) b2;
b1.test();
b3.test();
}
}
//output:
C
C
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 2 months, 3 weeks agosina_
1 year, 5 months agojackymak
1 year, 6 months agojackymak
1 year, 5 months agoMontassarTrablelsi
1 year, 5 months ago