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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:03:47+00:00 2026-05-20T18:03:47+00:00

So, in a single parent inheritance model what’s the best solution for making code

  • 0

So, in a single parent inheritance model what’s the best solution for making code extensible for future changes while keeping the same interface (I’d like to emphasize the fact that these changes cannot be known at the time of the original implementation, the main focus of my question is to explore the best mechanism/pattern for supporting these changes as they come up)? I know that this is a very basic OO question and below I provide example of how I’ve been going about it, but I was wondering if there a better solution to this common problem.

Here’s what I’ve been doing (the example code is in Java):

In the beginning, the following two classes and interface are created:

public class Foo
{
    protected int z;
}

public interface FooHandler
{
    void handleFoo(Foo foo);
}

public class DefaultFooHandler implements FooHandler
{
    @Override
    public void handleFoo(Foo foo)
    {
        //do something here
    }
}

The system uses variables/fields of type FooHandler only and that object (in this case DefaultFooHandler) is created in a few, well-defined places (perhaps there’s a FooHandlerFactory) so as to compensate for any changes that might happen in the future.

Then, at some point in the future a need to extend Foo arises to add some functionality. So, two new classes are created:

public class ImprovedFoo extends Foo
{
    protected double k;
}

public class ImprovedFooHandler extends DefaultFooHandler
{
    @Override
    public void handleFoo(Foo foo)
    {
        if(foo instanceof ImprovedFoo)
        {
            handleImprovedFoo((ImprovedFoo)foo);
            return;
        }
        if(foo instanceof Foo)
        {
            super.handleFoo(foo);
            return;
        }
    }

    public void handleImprovedFoo(ImprovedFoo foo)
    {
        //do something involving ImprovedFoo
    }
}

The thing that makes me cringe in the example above is the if-statements that appear in ImprovedFooHandler.handleFoo

Is there a way to avoid using the if-statements and the instanceof operator?

  • 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-20T18:03:48+00:00Added an answer on May 20, 2026 at 6:03 pm

    First of all the code you wrote won’t work.
    Each time you see instanceof and if...else together be very careful. The order of these checks is very important. In your case you’ll never execute handleImpovedFoo. Guess why 🙂

    It’s absolutely normal you have these instanceof statements. Sometimes it’s the only way to provide different behavior for a subtype.
    But here you can use another trick: use simple Map. Map classes of foo-hierarchy to instances of fooHandler-hierarchy.

    Map<Class<? extends Foo>, FooHandler> map ...
    
    map.put( Foo.class, new FooHandler() );
    map.put( ImprovedFoo.class, new ImprovedFooHandler() );
    
    Foo foo ...; // here comes an unknown foo 
    
    map.get( foo.getClass() ).handleFoo( foo );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be the best way to debug multi-process code (i.e. processes where parent
I created a single page (with code behind .vb) and created Public intFileID As
I'm currently using ActiveRecord single table inheritance. How can I cast one of my
Single inheritance is easy to implement. For example, in C, the inheritance can be
I am using Single Table Inheritance and have comments on all the subclasses. I
Is there explicit support for Single Table Inheritance in Django? Last I heard, the
Let's say Student is the parent class in an inheritance hierarchy that includes ComputerScienceStudent
i have a parent entity Service and a child ExtendedService in a SINGLE_TABLE inheritance.
I have a base class inherited by 2 others via Single Table Inheritance. I
I'm using nested_form for a situation where: Parent (climb) ==has_one==> Join Model (route_ascent) ==polymorphic

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.