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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:53:53+00:00 2026-05-29T04:53:53+00:00

I am very new to Java generics and have spent an exorbitant amount of

  • 0

I am very new to Java generics and have spent an exorbitant amount of time looking for an appropriate solution (if any).

I’m trying to design adapters that handle objects of a specific type of class. As described below, the CAdapter class handles “CClass” objects only. I am trying to provide an abstract generic adapter that handles the bulk of the work (much like Java collections such as LinkedList). I then provide a concrete adapter implementation for each type that needs to be supported.

// Classes

public interface AInterface {
  public String toString();
}

public class BClass extends AInterface  {
  public String toString() { return "BClass "; }
}

public class CClass extends AInterface  {
  public String toString() { return "CClass"; }
}

// Adapters

public interface AdapterInterface<T extends AInterface> {
  public T getInterface();
}

public class BAdapter implements AdapterInterface<BClass> {
  private BClass aInterface = null;
  public BClass getInterface() { return aInterface; }
}

public class CAdapter implements AdapterInterface<CClass> {
  private CClass aInterface = null;
  public CClass getInterface() { return aInterface; }
}

Firstly, I have read that providing a CONCRETE implementation for such a generic adapter is frowned upon (something about God killing a kitten)! Maybe somebody could expand on this?

Secondly, I have run into an issue with dynamically instantiating an adapter and not having the Java compiler complain. For example, I have the method:

public <T extends AInterface> AdapterInterface<T> getAdapter(String type) {
  AdapterInterface<T> result = null;
  if (type.equals("C") {
    result = new CAdapter();
  }
  return result;
}

Of course, the compiler will complain about CAdapter not matching . Given any type of AInterface object I would like to be able to load the right adapter and process it appropriately. I am failing to understand the factory pattern in order to accomplish this.

Any thoughts would be greatly appreciated.

  • 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-29T04:53:57+00:00Added an answer on May 29, 2026 at 4:53 am

    My answer is a bit superfluous, but:

    Anything with a <T> means “my caller knows what this type is, but I don’t”. So

    AdapterInterface<T> result = null;
    

    Means “I don’t actually know what type result is, it’s whatever my caller thinks it is”. The compiler complains about this:

    result = new CAdapter();
    

    Because the this code can’t assume that T is CClass.

    In fact, there is no way to do this without a cast (declaring the method wild carded just means that you need to cast the result where you call it). The cast is your way of telling the compiler “I know that you have no way of knowing what this is, that’s ok: I do. Trust me. Chill.”. Yes, you’ll get a warning. And that’s ok.

    Generics don’t eliminate all casting, but they allow you to do it just once. Instead of needing casts all over the place, you need them at just that one spot where you – the coder – know for sure that you are playing games with types. All the rest of the code, the suff that uses the adapter you have just created, can safely work with the generic type.

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

Sidebar

Related Questions

I am very new to Java and also Real Time Program. I am trying
I am very new to processing.org and Java. I am trying to store objects
I am very new to spring and java. I have been using mostly springsource.org
I am very new to JAVA. I have written simple program (in Linux -VIM
I am very new to Java Me (first time). I want my program to
Very new to Java and Swing, and I have been playing with a swing
I'm still very new to Java. I'm trying to use CMU's Sphinx4 voice recognition.
I am very new to java and spring. I was trying out my first
I am very new to Java, I want to add a PNG image to
I'm very new to Java but I've been developing a habit to use final

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.