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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:44:00+00:00 2026-06-07T21:44:00+00:00

I have class structure like this: class A1,A2,..,An extends A; class B1,B2,..,Bn extends B;

  • 0

I have class structure like this:

class A1,A2,..,An extends A;
class B1,B2,..,Bn extends B;

And class that converts Ai into Bi:

    private B1 convert(A1 a1){}
...
    private Bn convert(An an){}

How can I define single public method with signature like <? extends B> convert(<? extends A> a)?
Now I have only this approach:

    B convert(A a){
      if(A.getClass().equals(A1.class)){
         return convert((A1)a);
      }...
    }

Can I use instanceof if perfomance is important and the method will be called frequently?

  • 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-07T21:44:02+00:00Added an answer on June 7, 2026 at 9:44 pm

    A more elegant solution will probably be to declare a method in A: [preferably abstract, if A is abstract]:

    public abstract B toB();
    

    Overriding classes (A1,A2,…) will have to override it and instantiate their own B object.

    Code snap [the static modifier is used since I implemented it as an inner class, it is not needed and cannot be used if the classes are outer classes]:

    public abstract static class A { 
        public abstract B toB();
    }
    public static class A1 extends A {
        @Override
        public B1 toB() {
            return new B1();
        } 
    
    }
    
    public static class B {
    
    }
    public static class B1 extends B { 
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a files structure like this: Class (folder): - User.php - Rule.php Scripts
I have classes structured like this: Public MustInherit Class A ' several properties End
I have a class structure like abstract class Animal { public Animal(){ //init stuff..
Say I have a structure like: class SomeObject Public Name as String Public Created
I have the following class structure public class Outer{ private Mapper a; .... private
Let's see if I can describe this properly... I have an abstract class that
I have the following class structure: public abstract class Generic<T extends SuperClass> public class
I have this class structure UserInterface --- abstract Class User extends UserInterface class Teacher
I have a class whitch extends JPanel: public class ButtonPanel extends JPanel { private
I have class like following: public class Tree<T extends Comparable<? super 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.