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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:55:42+00:00 2026-06-07T14:55:42+00:00

I have to handle two classes with identical methods but they don’t implement the

  • 0

I have to handle two classes with identical methods but they don’t implement the same interface, nor do they extend the same superclass. I’m not able / not allowed to change this classes and I don’t construct instances of this classes I only get objects of this.
What is the best way to avoid lots of code duplication?

One of the class:

package faa;

public class SomethingA {

    private String valueOne = null;
    private String valueTwo = null;

    public String getValueOne() { return valueOne; }
    public void setValueOne(String valueOne) { this.valueOne = valueOne; }

    public String getValueTwo() { return valueTwo; }
    public void setValueTwo(String valueTwo) { this.valueTwo = valueTwo; }
}

And the other…

package foo;

public class SomethingB {

    private String valueOne;
    private String valueTwo;

    public String getValueOne() { return valueOne; }
    public void setValueOne(String valueOne) { this.valueOne = valueOne; }

    public String getValueTwo() { return valueTwo; }
    public void setValueTwo(String valueTwo) { this.valueTwo = valueTwo; }
}

(In reality these classes are larger)

My only idea is now to create a wrapper class in this was:

public class SomethingWrapper {

    private SomethingA someA;
    private SomethingB someB;

    public SomethingWrapper(SomethingA someA) {
        //null check..
        this.someA = someA;
    }

    public SomethingWrapper(SomethingB someB) {
        //null check..
        this.someB = someB;
    }

    public String getValueOne() {
        if (this.someA != null) {
            return this.someA.getValueOne();
        } else {
            return this.someB.getValueOne();
        }
    }

    public void setValueOne(String valueOne) {
        if (this.someA != null) {
            this.someA.setValueOne(valueOne);
        } else {
            this.someB.setValueOne(valueOne);
        }
    }

    public String getValueTwo() {
        if (this.someA != null) {
            return this.someA.getValueTwo();
        } else {
            return this.someB.getValueTwo();
        }
    }

    public void setValueTwo(String valueTwo) {
        if (this.someA != null) {
            this.someA.setValueTwo(valueTwo);
        } else {
            this.someB.setValueTwo(valueTwo);
        }
    }
} 

But I’m not realy satisfied with this solution. Is there any better / more elegant way to solve this problem?

  • 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-07T14:55:44+00:00Added an answer on June 7, 2026 at 2:55 pm

    A better solution would be to create an interface to represent the unified interface to both classes, then to write two classes implementing the interface, one that wraps an A, and another that wraps a B:

    public interface SomethingWrapper {
        public String getValueOne();
        public void setValueOne(String valueOne);
        public String getValueTwo();
        public void setValueTwo(String valueTwo);
    };
    
    public class SomethingAWrapper implements SomethingWrapper {
    
        private SomethingA someA;
    
        public SomethingWrapper(SomethingA someA) {
            this.someA = someA;
        }
    
        public String getValueOne() {
            return this.someA.getValueOne();
        }
    
        public void setValueOne(String valueOne) {
            this.someA.setValueOne(valueOne);
        }
    
        public String getValueTwo() {
            return this.someA.getValueTwo();
        }
    
        public void setValueTwo(String valueTwo) {
            this.someA.setValueTwo(valueTwo);
        }
    };
    

    and then another class just like it for SomethingBWrapper.

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

Sidebar

Related Questions

I have two classes; Area and LanguageArea . I use LanguageArea to handle translated
I have two HANDLEs and they are created from the same file, in such
I have two virtual machines with seemingly identical configuration (created from the same VM
I'm writing two classes to handle simple auctions. I have a class ready and
I have two functions that do the basically same thing on two different classes....
I actually have two questions regarding the same problem but I think it is
I have an input box which I want to handle two types of information,
I need to write data into drive. I have two options: write raw sectors.(_write(handle,
Say i have the following two classes: public class User { public int ID
I was wondering if you could help me out.. I have two classes, one

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.