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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:10:32+00:00 2026-06-18T07:10:32+00:00

I’m trying to get my head round how Java selects which method is executed:

  • 0

I’m trying to get my head round how Java selects which method is executed:

//Example 1 prints Square:add(Figure)
Figure fs = new Square();
fs.add(fs);

//Example 2 prints Square:add(Figure)
Rectangle rs = new Square();
rs.add(fs);

//Example 3 prints Rectangle:add(Rectangle). Expected Square:add(Square)
rs.add(new Square());

//Example 4 prints Rectangle:add(Rectangle). Expected Square:add(Figure)
Square ss = new Square();
ss.add(rs);

class Figure
{
    public void add(Figure f){ System.out.println("Figure:add(Figure)"); }
}

class Rectangle extends Figure
{
    @Override
    public void add(Figure f){ System.out.println("Rectangle:add(Figure)"); }
    public void add(Rectangle r){ System.out.println("Rectangle:add(Rectangle)"); }
}

class Square extends Rectangle
{
    @Override
    public void add(Figure f){ System.out.println("Square:add(Figure)"); }
    public void add(Square s){ System.out.println("Square:add(Square)"); }
}

What I’ve learned here is

  • Method signature gets determined based on compile time data types
  • Actual method invoked depends on the dynamic type of the object the method is called on.

Based on that, the result of the first two calls is as expected. However, I don’t understand the result of example 3 and 4.

It seems to be specified in the java language specification, but I don’t understand it.

  • 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-06-18T07:10:33+00:00Added an answer on June 18, 2026 at 7:10 am

    However, I don’t understand the result of example 3 and 4.

    Okay, let’s look at them individually.

    Example 3

    //Example 3 prints Rectangle:add(Rectangle). Expected Square:add(Square)
    rs.add(new Square());
    

    The important parts are the compile-time types of the expressions rs and new Square().

    rs is only declared as Rectangle, so the compiler will look at the methods declared by Rectangle and its superclasses:

    public void add(Figure f)
    public void add(Rectangle r)
    

    The type of the expression new Square() is Square, so both methods are applicable – but the second one is more specific.

    So the compiler will call add(Rectangle) on the object that rs refers to. That’s it for the compile-time side.

    At execution time, the value of rs refers to an instance of Square – but Square doesn’t override add(Rectangle) so the method picked is the implementation in Rectangle:

    public void add(Rectangle r){ System.out.println("Rectangle:add(Rectangle)"); }
    

    Example 4

    //Example 4 prints Rectangle:add(Rectangle). Expected Square:add(Figure)
    Square ss = new Square();
    ss.add(rs);
    

    Again, let’s consider the compile-time types involved… ss is of type Square, and rs is of type Rectangle (compile-time types, remember).

    The methods declared by Square and its superclasses are:

    public void add(Figure f)
    public void add(Rectangle r)
    public void add(Square s)
    

    As the compile-time type of rs is only Rectangle (not Square), the first two methods are applicable, but the third isn’t. Therefore, again, add(Rectangle) is picked at compile time (as it’s more specific than add(Figure)).

    Again, the execution time type of ss is Square, which doesn’t override add(Rectangle), so the implementation in Rectangle is used.

    Let me know if anything in here is confusing – if you can be specific about which part, that would be great.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:

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.