Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6643971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:06:47+00:00 2026-05-26T00:06:47+00:00

My code looks sort of like this, but this is a simplified version: class

  • 0

My code looks sort of 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()); // prints A
        (new B()).testArgs(new B());  // prints B
        a(new B()).test();            // prints B
    }
    public static A a(B b){
        return b;
    }
}

Why does a(new B()).testArgs(new B()) print A not B?
Is there some sort of way to workaround/fix this?

edit:
Clarification:

What I really want is the superclass method to be run when it is called with an A, and the subclass method to be run when testArgs is called with a B.

Casting also isn’t an option because in the actual code, unlike here, I don’t know whether the result of the method call is actually B or not.

edit:
Solution:

Thanks everyone for your answers. Thanks for the clarification on overriding. I used this to implement the desired behavior.
For anyone who has a similar problem in the future:
Change class B to

public class B extends A{
    public void testArgs(A a){       // Corrected overriding, thanks
        if(a instanceof B)           // Check if it is an instance of B
            System.out.println("B"); // Do whatever
        else                         // Otherwise
            super.testArgs(a);       // Call superclass method
    }
    public void test(){
        System.out.println("B");
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:06:47+00:00Added an answer on May 26, 2026 at 12:06 am

    The two testArgs functions are different. One takes an A and the other takes a B. Thus, the B version doesnt override the A version. Since a(new B()) is of type A and B extends A, it is the A version that will run.

    There is a “workaround”:

    public class B extends A{
    public void testArgs(A a){ // <-- note how this is A a, not B a 
        System.out.println("B");
    }
    public void test(){
        System.out.println("B");
    }
    }
    

    Then you will see B for all 3 cases (because B‘s testArgs will override A‘s testArgs)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A cut down version of my code sort of looks like this: string=test test
The code looks like below: namespace Test { public interface IMyClass { List<IMyClass> GetList();
My code looks like this NSNumber *inputToNumber = [NSNumber numberWithFloat:[textField.text floatValue]]; the value from
My code looks like this : Vector<String> My_Vector=new Vector<String>(); String My_Array[]=new String[100]; for (int
My code looks like this: NSIndexPath *ip = [NSIndexPath indexPathForRow:15 inSection:0]; [[self tableView] selectRowAtIndexPath:ip
At the moment my code looks like this: # Assign values for saving to
here is what my code looks like i have two forms: class Form_1 extends
I want to sort an array that looks like this (to numerical order instead
My code looks like: Entity e = new Entity(); e.name = ...; e.blah =
My current code looks like the following. How can I pass my array to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.