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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:29:31+00:00 2026-06-12T09:29:31+00:00

Let’s say I have the following classes: public interface X { … } public

  • 0

Let’s say I have the following classes:

public interface X {
...
}

public class A implements X {
...
}

public class B implements X {
...
}

Now let’s say I have a method somewhere that takes an object of type X, and must handle each implementation of X differently. The naïve approach is:

public void doSomething(X x) {
  if(x instanceof A)
    doSomethingA((A) x);
  else if(x instanceof B)
    doSomethingB((B) x);
}

…but this seems particularly ugly and not polymorphic. What is the clean way of handling such a situation in general?

EDIT: Sure, it would be easy if I could push the implementation of doSomething() to classes A and B, but what about in situations where that doesn’t make sense? i.e. doSomething() is defined on a class C and the implementation is highly dependent on C’s internal state.

  • 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-12T09:29:32+00:00Added an answer on June 12, 2026 at 9:29 am

    It’s easy:

    public interface X {
      int doSomething();
    }
    
    public class A implements X {
      public int doSomething() {
        // implementation in A
      }  
    }
    
    public class B implements X {
      public int doSomething() {
        // implementation in B
      }  
    
    }
    

    UPDATE: Ok, seems here we have some algorithm in C which dependants on C’s state as well as on A/B differences. Then I would try to split that algorithm so that C class has only a common implementation for both A and B, and A-/B-dependent parts should go to an appropriate class as a particular implementation of the same method which is invoked in C. If possible, C’s state can partially be passed to A’s and B’s doSomething

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

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let's say that I've the following main activity: public class MwConsoleActivity extends Activity {
Let me explain best with an example. Say you have node class that can
Let's say you have a method that expects a numerical value as an argument.
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say you have a class, with certain properties, and that you tried your
Let's say I have a method in java, which looks up a user in
Let's say that I have a SQLite database that I create in a separate
Let's say we have a standard Ajax form with the following Syntax: @using(Ajax.BeginForm(AddCityJson, City,new
Let's say we have the following jQuery plugins $.accordion(); $.button(); and in a script

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.