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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:21:53+00:00 2026-05-21T16:21:53+00:00

In the code below, how does first and second print statements print out SubObj??

  • 0

In the code below, how does first and second print statements print out SubObj??
Do top and sub point to the same Sub class?

class Top {
    public String f(Object o) {return "Top";}
}

class Sub extends Top {
    public String f(String s) {return "Sub";}
    public String f(Object o) {return "SubObj";}
}

public class Test {
    public static void main(String[] args) {  
        Sub sub = new Sub();
        Top top = sub;
        String str = "Something";
        Object obj = str;


        System.out.println(top.f(obj));
        System.out.println(top.f(str));
        System.out.println(sub.f(obj));
        System.out.println(sub.f(str));
    }
}

Above code returns below result.

SubObj
SubObj
SubObj
Sub
  • 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-21T16:21:54+00:00Added an answer on May 21, 2026 at 4:21 pm

    Since you already understand case 1, 3, and 4, let’s tackle case 2.

    (Please note – I am by no means an expert on the inner workings of the JVM or compilers, but this is how I understand it. If someone reading this is a JVM expert, feel free to edit this answer of any discrepancies you may find.)

    A method in a subclass that has the same name but a different signature is known as method overloading. Method overloading uses static binding, which basically means that the appropriate method will be forced to be “chosen” (i.e. bound) at compile-time. The compiler has no clue about the runtime type (aka the actual type) of your objects. So when you write:

                             // Reference Type  // Actual Type
        Sub sub = new Sub(); // Sub                Sub
        Top top = sub;       // Top                Sub
    

    the compiler only “knows” that top is of type Top (aka the reference type). So when you later write:

        System.out.println(top.f(str)); // Prints "subobj"
    

    the compiler “sees” the call ‘top.f’ as referring to the Top class’s f method. It “knows” that str is of type String which extends Object. So since 1) the call ‘top.f’ refers to Top class’s f method, 2) there is no f method in class Top that takes a String parameter, and 3) since str is a subclass of Object, the Top class’s f method is the only valid choice at compile time. So the compiler implicitly upcasts str to its parent type, Object, so it can be passed to Top’s f method. (This is in contrast to dynamic binding, where type resolution of the above line of code would be deferred until runtime, to be resolved by the JVM rather than the compiler.)

    Then at runtime, in the above line of code, top is downcast by the JVM to it’s actual type, sub. However, the argument str has been upcast by the compiler to type Object. So now the JVM has to call an f method in class sub that takes a parameter of type Object.

    Hence, the above line of code prints “subobj” rather than “sub”.

    For another very similar example, please see: Java dynamic binding and method overriding

    Update: Found this detailed article on the inner workings of the JVM:

    http://www.artima.com/underthehood/invocationP.html

    I commented your code to make it more clear what’s going on:

    class Top {
        public String f(Object o) {return "Top";}
    }
    
    class Sub extends Top {
        public String f(String s) {return "Sub";} // Overloading = No dynamic binding
        public String f(Object o) {return "SubObj";} // Overriding = Dynamic binding
    }
    
    public class Test {
        public static void main(String[] args) {  
    
                                      // Reference Type     Actual Type
            Sub sub = new Sub();      // Sub                Sub
            Top top = sub;            // Top                Sub
            String str = "Something"; // String             String
            Object obj = str;         // Object             String
    
                                            // At Compile-Time:      At Run-Time:
            // Dynamic Binding
            System.out.println(top.f(obj)); // Top.f (Object)   -->  Sub.f (Object)
    
            // Dynamic Binding
            System.out.println(top.f(str)); // Top.f (Object)   -->  Sub.f (Object)
    
            // Static Binding
            System.out.println(sub.f(obj)); // Sub.f (Object)        Sub.f (Object)
    
            // Static Binding
            System.out.println(sub.f(str)); // Sub.f (String)        Sub.f (String)
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Why does the code below return true only for a = 1? main(){ int
In the code below, why does the open function work but the close function
In the below code sample, what does {0:X2} mean? This is from the reflection
The code below works. But if I comment out the line Dim objRequest As
Why does the below code prints 2147483647, the actual value being 2147483648? i =
Code below is not working as expected to detect if it is in design
The code below shows a sample that I've used recently to explain the different
The code below gives me this mysterious error, and i cannot fathom it. I
The code below gives an error: Property 'Int32 Key' is not defined for type

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.