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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:45:47+00:00 2026-05-22T20:45:47+00:00

Maybe I am having some problems understanding generics, but I can’t do something that

  • 0

Maybe I am having some problems understanding generics, but I can’t do something that looks logical to me. Maybe you can clarify it for me.

I am developing a Java Library and there is a function that will call a platform that will return information in the JSON format. I wanted to create a namespace responsible for retrieving and parsing the JSON information into business objects. To do that, I created this:

JSON class – Has 2 methods:

protected JSONObject readJsonFromUrl(String url)

open the stream and download the JSON file

public <T, U extends Parser> T readObjectFromUrl(String url, U parser) 

is the “entry point”. Will call the readJsonFromUrl, and call the returned value to the parser.

The Parser class is an abstract class and has this interface

public abstract class Parser {

public abstract <T> T parse(JSONObject jsonObject);
}

What I wanted to do is to create a subclass that overrides the parse method, each subclass would return a different type.
For instance, a contact list parser would look like this:

public <T> T parse(JSONObject jsonObject) {
ContactList contactList = new ContactList();

    //Simplified for clearness
    contactList = parse(jsonObject);

return contactList;
}

The problem is: I get a compiler error because it is expecting a T, and not a contactList.
If I change to , the signature of the new method do not match the signature of the parent class.
If I change the contactList’s declaration to T contactList;, I can’t call some methods I need (for instance addContact).

Am I getting generics wrong? Are they suited for what I want in this case?
If they are not, how would you implement a similar functionality?

Thanks,
Oscar

Edit: is the best solution to use Object instead of generics? It doesn’t seem so pretty 🙁

  • 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-22T20:45:47+00:00Added an answer on May 22, 2026 at 8:45 pm

    You’re using generics on a method which returns always the same type of Objects, and that type may vary depending on the implementation of the method.

    Then I guess you should move your generic declaration to the class.

    public abstract class Parser<T> {
        public abstract T parse(JSONObject jsonObject);
    }
    

    And your implementation will look like :

    public class Impl extends Parser<ContactList>{
        public abstract ContactList parse(JSONObject jsonObject){
            ContactList contactList = new ContactList();
            //Simplified for clearness
            contactList = parse(jsonObject);
            return contactList;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble groking something in Linq - maybe someone can give me some
I'm currently developing a site ( http://landscaping.jvsoftware.com/ ) but I'm having some strange problems
Not too practical maybe, but still interesting. Having some abstract question on matrix multiplication
Maybe I'm having a bad day, but I can't seem to combine these 2
Maybe I am having a moment of 'afternoon', but can anyone explain why I
I'm having some problems with the SqlNamedQuery attribute that has been added to the
Maybe I'm having a really bad day, but could someone possibly help me to
Maybe you can help me with a problem I am having. The URL of
Maybe I'm just thinking about this too hard, but I'm having a problem figuring
I know this maybe a very basic question but I'm having a bit of

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.