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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:43:40+00:00 2026-05-27T10:43:40+00:00

I have two classes, Foo1 and Foo2 that are not hierarchically related, so an

  • 0

I have two classes, Foo1 and Foo2 that are not hierarchically related, so an instance of Foo1 is not an instance of Foo2 nor conversely.

In a different class from these, I have a method doStuffWithFoo that ideally should take either Foo1 or Foo2 as input, and then behave accordingly. Here’s the basic idea that I’m after:

private double[] doStuffWithFoo(Fooi foo, Bar bar1, Bar bar2, double fortyTwo) {
    Fooi changingFoo = foo;
    double[] fooBars = new double[N];
    for (int i=0; i<N; ++i) {
        double[] array = getArrayFromFoo(foo,bar1,fortyTwo);
        double fooBar = getDoubleFromArray(array);
        fooBars[i] = fooBar;
        foo = foo.getNextFoo();
    }
    return fooBars;
}

where I’ve written two very different methods

private double[] getArrayFromFoo(Foo1 foo, Bar bar, double fortyTwo)
private double[] getArrayFromFoo(Foo2 foo, Bar bar, double fortyTwo)

and each Foo class implements its own getNextFoo() method that returns the same type of Foo that it takes in.

The method getArrayFromFoo doesn’t really have anything to do with the Foo classes and it takes input from the class where this method lives, so it doesn’t make sense to create a common interface for the Foos.

The ways I see to handle the situation are:

  1. Make doStuffWithFoo take a generic object as input, then check whether the class is Foo1, Foo2, or something else. The problem with this approach is that I strongly prefer compile time errors to run time errors.

  2. Write two versions of doStuffWithFoo just as I wrote two versions of getArrayFromFoo. The problem with this approach is that I’ll be duplicating a large amount of code which may cause inconsistencies when the code is changed at some point down the line.

Is there any other option? The ideal solution I have in mind is if there is a way to declare the input type to be Foo1 or Foo2, which is like approach 1 but with instant errors. Is there a way to do this in Java?

  • 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-27T10:43:41+00:00Added an answer on May 27, 2026 at 10:43 am

    What about this:

    private double[] doStuffWithFoo(Foo1 foo) {
        double[] array = getArrayFromFoo1(foo);
        doCommonStuff(array);
        return array;
    }
    
    
    private double[] doStuffWithFoo(Foo2 foo) {
        double[] array = getArrayFromFoo2(foo);
        doCommonStuff(array);
        return array;
    }
    
    private void doCommonStuff(double[] array) {
        // common stuff
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes(Book,Software) that inherit from the Product class. these two classes have
I have two classes, and want to include a static instance of one class
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two classes that each need an instance of each other to function.
I have two classes that my pages and controls inherit from. These two classes
I have two classes: Action class, that has a method for executing VBScript files,
I have two classes that are already subclasses of a different parent (due to
I have two classes, Person and Company, derived from another class Contact. They are
I have two classes that can act as a delegate of a third class,
I have two classes defined in different h files and each class has some

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.