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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:07:19+00:00 2026-05-24T01:07:19+00:00

I have been trying to generate a Factory supposed to return a different object

  • 0

I have been trying to generate a Factory supposed to return a different object of a common interface (say Item) according to the input parameter (I call it a context) of the function getItem(A context)

Now, assume I define a new type of context: B which inherits from A.

I wanted to return a different item depending on whether the object passed to the factory was of class B or A.

I tried to do as follows (overloading the method):

class Factory
{
   static Item getItem(A context) {...}
   static Item getItem(B context) {...}
}

This works fine if I do something like this:

B bContext=new B();
Item it=Factory.getItem(bContext);

However, if I cast and object to type A:

A bContext=(A) new B();
Item it=Factory.getItem(bContext);

the first factory method is called.

I thought that polymorphism would ensure the execution of the second method even after the cast, and I would like to know if I missed something?

I am aware that I could keep having a single method and use the is operator to check what the type of the variable is, but I thought the solution I presented above was a bit more elegant.

  • 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-24T01:07:20+00:00Added an answer on May 24, 2026 at 1:07 am

    Overloading is decided at compile-time (aside from using dynamic typing in C# 4) based on the compile-time type of the arguments – and in your last snippet, the compile-time type of the argument is A, so it calls Factory.getItem(A).

    Only virtual method calls are polymorphic (using overriding), where the actual execution-time type of the target object to decide which implementation to call. If it makes sense for A and B to have a virtual method (overridden in B) which can be called by Factory.getItem to handle the differences, that’s great… otherwise you’re stuck with either dynamic typing or something like is.

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

Sidebar

Related Questions

I have been trying to generate simple PDFs from my app so that I
I need to generate uncompressible data (therefore pseudorandom), and have been trying with the
I am playing around with Audacity and have been trying to generate tones with
I have been trying to generate data in Excel. I generated .CSV file. So
I have been trying to use XStreamMarshaller to generate XML output in my Java
I have been trying to return an ARABIC string from a JNI call. The
I have been trying in vain for almost two weeks now to generate a
I have been trying to generate a heatmap in R for some microarray data
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I have been trying to find a really fast way to parse yyyy-mm-dd [hh:mm:ss]

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.