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

  • Home
  • SEARCH
  • 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 166735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:09:00+00:00 2026-05-11T12:09:00+00:00

I am practicing for an exam, and found a sample problem that gets me

  • 0

I am practicing for an exam, and found a sample problem that gets me totally lost. For the following code, find what the output is:

class Moe {     public void print(Moe p) {         System.out.println('Moe 1\n');     } } class Larry extends Moe {     public void print(Moe p) {         System.out.println('Larry 1\n');     }     public void print(Larry l) {         System.out.println('Larry 2\n');     } } class Curly extends Larry {     public void print(Moe p) {         System.out.println('Curly 1\n');     }     public void print(Larry l) {         System.out.println('Curly 2\n');     }     public void print(Curly b) {         System.out.println('Curly 3\n');     } } public class Overloading_Final_Exam {     public static void main (String [] args) {         Larry stooge1 = new Curly();         Moe stooge2 = new Larry();         Moe stooge3 = new Curly();         Curly stooge4 = new Curly();         Larry stooge5 = new Larry();         stooge1.print(new Moe());          ((Curly)stooge1).print(new Larry());          ((Larry)stooge2).print(new Moe());          stooge2.print(new Curly());          stooge3.print(new Curly());          stooge3.print(new Moe());          stooge3.print(new Larry());          ((Curly)stooge3).print(new Larry());          ((Curly)stooge3).print(new Curly());          stooge4.print(new Curly());          stooge4.print(new Moe());          stooge4.print(new Larry());          stooge5.print(new Curly());          stooge5.print(new Larry());          stooge5.print(new Moe());      } } 

I had my ideas in mind, but then when I ran the java, I got something totally different:

 Curly 1 Curly 2 Larry 1 Larry 1 Curly 1 Curly 1 Curly 1 Curly 2 Curly 3 Curly 3 Curly 1 Curly 2 Larry 2 Larry 2 Larry 1 

The first few ones are OK, but then I really don’t understand. Anyone has a good explanation for this problem?

Thanks

  • 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. 2026-05-11T12:09:01+00:00Added an answer on May 11, 2026 at 12:09 pm

    I would start by drawing a picture…

    Moe - print(Moe)  | Larry - print(Moe), print(Larry)  | Curly - print(Moe), print(Larry), print(Curly) 

    Then I would keep track of the variables:

    • Larry – stooge1 -> Curly
    • Moe – stooge2 -> Larry
    • Moe – stooge3 -> Curly
    • Curly – stooge4 -> Curly
    • Larry – stooge5 -> Larry

    • stooge1.print(new Moe())

      • stooge1 -> Curly so calls Curly.print(Moe)
    • ((Curly)stooge1).print(new Larry());

      • stooge1 -> Curly so calls Curly.print(new Larry())
    • ((Larry)stooge2).print(new Moe());

      • stooge2 -> Larry so calls Larry.print(new Moe());
    • stooge2.print(new Curly());
      Ok, this is where it gets a bit trickier (sorry I stopped one before here)

      • stooge2 is declared to be a Moe. So when the compiler is looking at what to call it is going to call the print(Moe) method. Then at runtime it knows that stooge2 is a Larry so it calls the Larry.print(Moe) method.

    etc…

    Let me know if following that all the way through doesn’t work out for you.

    (updated to clarify the next one)

    So the general rule is:

    • the compiler looks at the variable type to decide what method to call.
    • the runtime looks at the actual class that the variable is point at to decide where to get the method from.

    So when you have:

    Moe stooge2 = new Larry(); stooge2.print(new Moe()); 

    the compiler says:

    • can Larry be assigned to stooge2? (yes as Larry is a subclass of Moe)
    • does Moe have a print(Moe) method? (yes)

    the runtime says:

    • I am supposed to call the print(Moe) method on this here object… stooge2
    • stooge2 is point at a Larry.
    • I’ll call the print(Moe) method in the Larry class.

    Once you have worked all that out try getting rid of some of the methods and see how that changes things.

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

Sidebar

Ask A Question

Stats

  • Questions 161k
  • Answers 161k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It depends on the platform, but the convention that ActiveRecord… May 12, 2026 at 11:47 am
  • Editorial Team
    Editorial Team added an answer you can pass in the output of one tag as… May 12, 2026 at 11:47 am
  • Editorial Team
    Editorial Team added an answer Are you looking for something along these lines? dispatch =… May 12, 2026 at 11:47 am

Related Questions

I am practicing using pointers. I have a pointer to an array of 3
Its been a while I have ready Mcconnell's Code Complete . Now I read
Hey guys. I have a method that gets called each second which I want
Are there any hard limits on the number of rows in a table in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.