My code looks something like this, but this is a simplified version:
class A:
public class A{ public void testArgs(A a){ System.out.println("A"); } public void test(){ System.out.println("A"); } }
class B:
public class B extends A{ public void testArgs(B a){ System.out.println("B"); } public void test(){ System.out.println("B"); } }
class Main:
public class Main{ public static void main(String[] args){ a(new B()).testArgs(new B());
Why is a(new B()).testArgs(new B())
prints A, not B? Is there a workaround / fix for this?
Editing:
Clarification:
I really want the superclass method to run when it is called with A, and the subclass method that should be run when testArgs is called from B.
Casting is also not an option, because in the actual code, unlike here, I do not know if the result of the method call is actually B or not.
Editing:
Decision:
Thank you all for your answers. Thanks for the clarification about the redefinition. I used this to implement the desired behavior.
For those who have a similar problem in the future:
Change class B to
public class B extends A{ public void testArgs(A a){
source share