I have a class named A, and a class named B that extends A. Playing with some methods to understand polymorphic behaviour, I ran into a weird situation.
public class Main {
public static void main(String[] args){
B b = new B();
A a = b;
b.f1(a);
}
}
public class A {
.
.
.
public void f1(A a){
if(a instanceof B)
f1((B)a);
else
System.out.println("Nothing");
}
.
.
.
}
public class B extends A {
.
.
.
public void f1(B b){
System.out.println("B::f1(B)");
}
.
.
.
}
I expected f1 in class A to be called first(because a is of type A) which actually happened. Then I expected the line f1((B)a); to be called, since a is an instance of B. Until now everything went as expected. However, I thought that the next method that will be called is f1(B) in class B. Instead, f1(A) in class A was called over and over causing a stack overflow exception. Why wasn't the f1(B) in class B called? An instance of B was the caller and the parameter was cast to type B.
f1(A a) is an instance method of class A. It has no knowledge of methods of sub-classes of A. Therefore, it cannot call void f1(B b) of class B. Therefore, f1((B)a) executes void f1(A a) again.
If you want to call f1(B b), you'll have to call f1 on an instance variable of class B:
public void f1(A a){
if(a instanceof B) {
B b = (B)a;
b.f1(b);
} else {
System.out.println("Nothing");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With