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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:55:34+00:00 2026-05-16T13:55:34+00:00

I have a lot of classes which are basically equivalent, ie have almost the

  • 0

I have a lot of classes which are basically equivalent, ie have almost the same functions in all respect. Let’s call them A and B. There are hundreds of methods that are using A and now due to code changes I have to change them to B.

Is there a way by which I can program a mapping between A and B such that I have minimal code changes?

  • 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-16T13:55:35+00:00Added an answer on May 16, 2026 at 1:55 pm

    If A is not final, you can make B extends A, and have B‘s methods @Override A‘s. Then wherever previously you were invoking methods on an instanceof A, you now provide an instanceof B, and let dynamic dispatch handle the rest.

    This is called polymorphism. It only works with non-static methods having the same exact signature. You can not @Override static methods.

    See also

    • Java Tutorials/Object-Oriented Programming Concepts
    • http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

    Related questions

    • Why doesn’t Java allow overriding of static methods ?

    On interfaces

    Depending on why you were doing this, you should know learn the concept of interfaces and how they’re used in object-oriented programming to allow precisely this kinds of flexibility and convenience.

    Consider the interface List<E>, for example, and an implementation ArrayList<E>. If you wrote an entire library that works with an ArrayList<E>, doing all the usual add/addAll/remove etc, and now you must use a LinkedList<E> instead, then you’d have little choice but to go to the source code and change all the ArrayList<E> to LinkedList<E>, and hope that the change doesn’t break another code which still assumed that ArrayList<E> was used.

    If instead your library works with a List<E>, then switching to a LinkedList<E> need to be done only wherever the objects are created. All the other code that was doing the add/addAll/remove would’ve still worked just fine, since those are methods that are defined in the interface List<E> which all implementors will have.

    It’s not clear from the current context, but if A and B are so similar, then perhaps they belong to some type X. If so, you should consider defining interface X, and have A implements X, and B implements X.

    See also

    • Java Tutorials/Object-Oriented Programming Concepts/Interfaces
    • Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces

    Related questions

    • Is it just me or are interfaces overused?
    • Why are interfaces preferred to abstract classes?
    • Explaining Interfaces to Students
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a lot of form classes all of which extend Form . I
Suppose, I have a lot of classes, which are constructed using Java reflection (for
i have an android project which has a lot of classes I've built. i
Let's we have a lot of such classes(millions) class WordInfo { string Value; string
I have to create a lot of very similar classes which have just one
I have a lot of classes on my project, my Source Editor displays the
I have a lot of comparer classes where the class being compared is simply
I've got a program where a lot of classes have really complicated configuration requirements.
I am beginning to define classes in JavaScript, and I have a lot of
I have to write a lot of helper classes for some reasons like below:

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.