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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:20:40+00:00 2026-06-08T12:20:40+00:00

I have two classes with identical implementations but different names. They are generated and

  • 0

I have two classes with identical implementations but different names. They are generated and I’m not able to modify them:

public class Foo {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class Bar {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

I need to populate these classes with data, and since they are identical would prefer to do it with a single class or reference, then cast to (or hopefully the solution is more elegant) the Foo or Bar as needed. Conceptually:

FooOrBar fob = ????;   
fob.setName("XYZ");

Foo f = fob.(???);
Bar b = fob.(???);

I’ve looked into generics, but it appears that is for when you truly don’t know what Class you will be operating on. Is it possible for me to have some type of Class or reference that achieves this?

July 26 EDIT:

Apologies if it’s bad form for follow ups like this, I can post new if need be. I’m in the process of implementing Tomasz’s Adapter pattern but my final step differs from his. I have a need for conditional assignment of the interface object. The below is what I have and I get no errors or warnings on it so far, but I’m wondering if this isn’t good practice for some reason? Perhaps I should use reflection or generics to determine the class for the interface? This will be in a method that only my code will call and I will know the condition. I’ve searched on conditional interface assignment but haven’t found much.

FooOrBar fob;  //interface

if (condition true) {

    fob = new FooAdapter();  //FooAdapter implements FooOrBar
} else {
    fob = new BarAdapter();  //BarAdapter implements FooOrBar
}

fob.setName("XYZ");
  • 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-08T12:20:41+00:00Added an answer on June 8, 2026 at 12:20 pm

    What you are looking for is called duck-typing and is not supported in Java. Try dynamic languages like groovy and jruby.

    In Java, assuming you can’t modify Foo and Bar, you’ll need an Adapter pattern. First create an abstraction over Foo and Bar:

    public interface FooOrBar {
        String getName();
        void setName(String name);
    }
    

    And have two adapters like these (BarAdapter is almost the same):

    public class FooAdapter implements FooOrBar {
    
        private final Foo foo;
    
        public FooAdapter(Foo foo) {
            this.foo = foo;
        }
    
        public String getName() {
            return foo.getName();
        }
    
        public void setName(String name) {
            foo.setName(name);
        }
    
    }
    

    Now you can say:

    FooOrBar fob = new FooAdapter(foo);
    FooOrBar fob2 = new BarAdapter(bar);
    
    fob.setName("XYZ");
    

    I’ll leave extracting the actual value to you (do you really need it?)

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

Sidebar

Related Questions

I have to handle two classes with identical methods but they don't implement the
I have two classes. They're almost identical, except for 2 attributes. I need to
Suppose I have two classes with identical members from two different libraries: namespace A
I have two identical classes with different properties and in different libraries. I reference
I have two classes A and B generated by cxf-codegen-plugin from my WSDL. A
Say I have two type classes defined as follows that are identical in function
So, lets say I have two nearly identical classes in C# and Ruby: C#
I have two classes like so: public class SentEmailAttachment : ISentEmailAttachment { public SentEmailAttachment();
I have two classes: class Player { public string Id { set; get; }
I have a relationship between two base classes: public abstract class RecruiterBase<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.