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

  • Home
  • SEARCH
  • 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 6708117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:43:15+00:00 2026-05-26T07:43:15+00:00

I have 2 classes e.g. A and B. These classes have a couple of

  • 0

I have 2 classes e.g. A and B.
These classes have a couple of getter/setter methods with the same name.

Now in the code I do the following:

if(obj.getClassName().equals(A.class.getName())){
   A a = (A) obj;
   String result = a.getInfo();
}
else if(obj.getClassName().equals(B.class.getName())){
   B a = (B) obj;
   String result = a.getInfo();
}

I was wondering if there is a way to call the getInfo avoiding the if statements.
Note: I can not refactor the classes to use inheritence or something else.
I was just interested if there is a trick in java to avoid the if statements.

  • 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-26T07:43:15+00:00Added an answer on May 26, 2026 at 7:43 am

    Unless you want to use reflection, no. Java treats two types which happen to declare the same method (getInfo()) as entirely separate, with entirely separate methods.

    If you’ve got commonality, you should be using a common superclass or a common interface that both of them inherit. You’ve tagged the question “design-patterns” – the pattern is to use the tools that the language provides to show commonality.

    As Eng.Fouad shows, using instanceof is simpler anyway – and better, as it means your code will still work with subclasses of A or B.

    You can isolate this ugliness, of course, by putting it in a single place – either with a facade class which can be constructed from either an A or a B, or by having a single method which performs this check, and then calling that from multiple places.

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

Sidebar

Related Questions

I have a couple of classes (for this example anyway) that use code first
I have a couple of classes, all derived from the same base type. class
I have these classes. class Author{ Person person } class Person{ String lastName String
I have these classes in C# (.NET Framework 3.5) described below: public class Base
I have these classes: class Base { ... private: std::vector<X> v; }; class Derived
Hi i have these classes: class Core < ActiveRecord::Base belongs_to :resource, :polymorphic => true
Concerning data binding I have these classes: public class Foo : List<Bar> { public
I have several classes such as Order, Customer, etc. These classes serve for holding
I have annotated my classes with @Repository, @Resource, @Component, @Service annotations but these classes
I have these two classes public class Person { } public class Company {

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.