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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:03:29+00:00 2026-06-16T13:03:29+00:00

Suppose I’ve got two classes A and B such that B extends A ,

  • 0

Suppose I’ve got two classes A and B such that B extends A, respectively implementing method getA and getB that has the same input parameter types and same return type (but different names). In a method public static <T extends A> void print(T t) getting as input an object of type A or B, I want to call getA if the object is of type A or getB if the object is of type B.

If I could edit the code of A and B I would make them implement an interface I providing getValue() (in the implementation for A and B, getValue() would call getA() and getB() resp.), and then call this method from print(). Problem is: classes A and B are not modifiable!. No methods can be added or changed, their class hierarchy is fixed, I didn’t write their code, and I don’t have access to the source.

Note that this also applies to the cases when – for any reason – I don’t want to change the code of A and B.

Is there any way of doing so without the use of instanceof?

Here follows a solution that uses instanceof (bad!).

public class Test {

    public static <T extends A> void print(T t){
        if (t instanceof B)
            System.out.println(((B)t).getB());
        else if (t instanceof A)
            System.out.println(t.getA());
    }

    public static class A {
        public String getA(){
            return "A";
        }
    }
    public static class B extends A {
        public String getB(){
            return "B";
        }
    }

}

  • 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-16T13:03:30+00:00Added an answer on June 16, 2026 at 1:03 pm

    To extend Matt’s answer, I would consider adopting the Facade Pattern for these classes. This means you create a separate class (or two) to wrap all the functionality you require from these two classes. This includes adding a method, as Matt suggests.

    The benefit is you decouple your application from an API you have no control over. You also have an opportunity to simplify the API, if you don’t require all the methods from the original libraries.

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

Sidebar

Related Questions

Suppose I have a number of related classes that all have a method like
Suppose I have a pure virtual method in the base interface that returns to
Suppose I have a static method of my class that returns an object of
Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose we have two tables. Post and Comment. Post has many Comments. Pretend they
Suppose I have two pages: - page 1 uses jquery mobile and has a
Suppose that I have an n -sided loaded die, where each side k has
Suppose that I have to draw two faces of a 3D shape in a
Suppose I have a DataContext object and access two tables at the same time:
Suppose I have a simple model, such as Record: @Model public class Record {

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.