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 6173019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:29:42+00:00 2026-05-23T23:29:42+00:00

01 class SubClass extends SuperClass {} 02 class AppSuperClass { 03 /** 04 *

  • 0
01    class SubClass extends SuperClass {}
02    class AppSuperClass {
03        /**
04         * @param superClass
05         */
06        public void print(SuperClass superClass) {
07            System.out.println("AppSuperClass:superclass is parameter");
08     
09        }
10        /**
11         * @param subClass
12         */
13        public void print(SubClass subClass) {
14            System.out.println("AppSuperClass:subclass is parameter");
15   
16        }
17    }
18   
19    class AppSubClass extends AppSuperClass {
20      /**
21       * @param superClass
22       */
23      public void print(SuperClass superClass) {
24          System.out.println("AppSubClass:superclass is parameter");
25   
26      }
27      /**
28       * @param subClass
29       */
30      public void print(SubClass subClass) {
31          System.out.println("AppSubClass:subclass is parameter");
32   
33      }
34  }
35  public class OverloadedTest {
36      public static void main(String[] args) {
37          AppSuperClass appSuperClass = new AppSuperClass();
38          AppSuperClass appSubClass = new AppSubClass();
39          SuperClass superClass = new SuperClass();
40          SuperClass subClassInstance = new SubClass();
41          /*
42           * Making request to print AppSuperClass
43           *  1. Passing SuperClass instance
44           *  2. Passing SubClass instance (*make note of the type) <img src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1304052800g" alt=":)" class="wp-smiley">
45           */
46   
47          appSuperClass.print(superClass);
48          appSuperClass.print(subClassInstance);
49          /*
50           * Above is repeated with AppSubClass instance
51           */
52          appSubClass.print(superClass);
53          appSubClass.print(subClassInstance);
54      }
55   
56  }

When I run this I get

       AppSuperClass:superclass is parameter
       AppSuperClass:superclass is parameter
       AppSubClass:superclass is parameter
       AppSubClass:superclass is parameter

How can I get

           AppSuperClass:superclass is parameter
           AppSuperClass:subclass is parameter
           AppSubClass:superclass is parameter
           AppSubClass:subclass is parameter

as o/p without changing the type of any of the objects?

  • 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-23T23:29:43+00:00Added an answer on May 23, 2026 at 11:29 pm

    Your question isn’t very clear, but it sounds like you’re basically after execution-time overloading, which simply doesn’t exist in Java. Overloads are entirely resolved at compile-time. One option is this:

    public void print(SuperClass superClass) {
        if (superClass instanceof SubClass) {]
            print((SubClass) superClass);
            return;
        }
        System.out.println("AppSuperClass:superclass is parameter");
    }
    

    Note that you’d need to do this in the override as well, or have a template method which does this, and a separate printImpl(SuperClass) method which can be overridden in the subclass.

    To take this to the logical extreme, you might have:

    class AppSuperClass {
    
        public final void print(SuperClass superClass) {
            if (superClass is SubClass) {
                printImpl((SubClass) superClass);
            } else {
                printImpl(superClass);            
            }
        }
    
        protected void printImpl(SuperClass superClass) {
            ...
        }
    
        protected void printImpl(SubClass subClass) {
           ...
        }
    }
    

    AppSubClass would then only override printImpl (one or both overloads).

    EDIT: As noted in comments, an alternative is to use the Visitor pattern. It’s not a pattern I’m terribly fond of, but if you can modify SuperClass and SubClass to know about AppSuperClass (or an interface it implements) it could work for you.

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

Sidebar

Related Questions

I have a class like this : public class SubClass <T> extends SuperClass<Collection<T>>{ public
Consider this code: class Test { Test() { System.out.println(In constructor of Superclass); } int
I have the following code: class SuperClass { public static String getName() { return
I get a NullPointerException calling a Superclass Method in Subclass Inner Class Constructor... What's
public class Foo extends Properties { public String getVal(){ return 2; } } In
Hi I am trying to find out the subclass, form an superclass object. if
I have some class inheritance SubClass < MidClass < SuperClass and want to perform
Running this piece of code will print null public class Weird { static class
What is use of creating Subclass reference to Superclass Object class A { /*
New class is a subclass of the original object It needs to be php4

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.