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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:39:33+00:00 2026-05-11T14:39:33+00:00

I asked a similar question yesterday that was specific to a technology, but now

  • 0

I asked a similar question yesterday that was specific to a technology, but now I find myself wondering about the topic in the broad sense.

For simplicity’s sake, we have two classes, A and B, where B is derived from A. B truly ‘is a’ A, and all of the routines defined in A have the same meaning in B.

Let’s say we want to display a list of As, some of which are actually Bs. As we traverse our list of As, if the current object is actually a B, we want to display some of Bs additional properties….or maybe we just want to color the Bs differently, but neither A nor B have any notion of ‘color’ or ‘display stuff’.

Solutions:

  1. Make the A class semi-aware of B by basically including a method called isB() in A that returns false. B will override the method and return true. Display code would have a check like: if (currentA.isB()) B b = currentA;

  2. Provide a display() method in A that B can override…. but then we start merging the UI and the model. I won’t consider this unless there is some cool trick I’m not seeing.

  3. Use instanceof to check if the current A object to be displayed is really a B.

  4. Just add all the junk from B to A, even though it doesn’t apply to A. Basically just contain a B (that does not inherit from A) in A and set it to null until it applies. This is somewhat attractive. This is similar to #1 I guess w/ composition over inheritance.

It seems like this particular problem should come up from time to time and have an obvious solution.

So I guess the question maybe really boils down to:

If I have a subclass that extends a base class by adding additional functionality (not just changing the existing behavior of the base class), am I doing something tragically wrong? It all seems to instantly fall apart as soon as we try to act on a collection of objects that may be A or B.

  • 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-11T14:39:33+00:00Added an answer on May 11, 2026 at 2:39 pm

    A variant of option 2 (or hybrid of 1 and 2) may make sense: after all, polymorphism is the standard solution to ‘Bs are As but need to behave differently in situation X.’ Agreed, a display() method would probably tie the model to the UI too closely, but presumably the different renderings you want at the UI level reflect semantic or behavioural differences at the model level. Could those be captured in a method? For example, instead of an outright getDisplayColour() method, could it be a getPriority() (for example) method, to which A and B return different values but it is still up to the UI to decide how to translate that into a colour?

    Given your more general question, however, of ‘how can we handle additional behaviour that we can’t or won’t allow to be accessed polymorphically via the base class,’ for example if the base class isn’t under our control, your options are probably option 3, the Visitor pattern or a helper class. In both cases you are effectively farming out the polymorphism to an external entity — in option 3, the UI (e.g. the presenter or controller), which performs an instanceOf check and does different things depending on whether it’s a B or not; in Visitor or the helper case, the new class. Given your example, Visitor is probably overkill (also, if you were not able/willing to change the base class to accommodate it, it wouldn’t be possible to implement it I think), so I’d suggest a simple class called something like ‘renderer’:

    public abstract class Renderer {   public static Renderer Create(A obj) {     if (obj instanceOf B)       return new BRenderer();     else       return new ARenderer();   }    public abstract Color getColor(); }  // implementations of ARenderer and BRenderer per your UI logic 

    This encapsulates the run-time type checking and bundles the code up into reasonably well-defined classes with clear responsibilities, without the conceptual overhead of Visitor. (Per GrizzlyNyo’s answer, though, if your hierarchy or function set is more complex than what you’ve shown here, Visitor could well be more appropriate, but many people find Visitor hard to get their heads around and I would tend to avoid it for simple situations — but your mileage may vary.)

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

Sidebar

Related Questions

I asked a similar question yesterday, but recognize that i need to rephase it
I asked a similar question earlier today, but now I am told that the
I asked a similar question yesterday, but now I have a slight twist. Since
I asked a similar question yesterday but I included some code that basically took
This question is about a similar javascript scope problem that I asked yesterday .
This is similar to a question I asked yesterday but more specific to the
I asked a similar question about this previously, but I did not specify that
I asked a similar question yesterday that was answered correctly. SQL requiring self join
I asked a similar question like this yesterday but after waiting for ever I
I asked a similar question previously, but it was so vague that I couldn't

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.