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

The Archive Base Latest Questions

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

(This is with Java 7 ) I was trying to put some JSON string-generating

  • 0

(This is with Java 7)

I was trying to put some JSON string-generating method in my base class rather than having near-identical code in all the subclasses. The first, naive thing I tried was:

public abstract class Base
{
    [rest of class...]

    final public <T extends Base> String toJsonString() throws IOException {
        JacksonRepresentation<T> rep =
             new JacksonRepresentation<>(MediaType.APPLICATION_JSON, this);
        return rep.getText();
    }    
}

But that wouldn’t compile, giving the error:

error: incompatible types
required: JacksonRepresentation<T>
found:    JacksonRepresentation<Base>
where T is a type-variable:
T extends Base declared in method <T>toJsonString()

So I tried this:

public abstract class Base
{
    [rest of class...]

    final public String toJsonString() throws IOException {
        return jsonStringHelper(this);
    }

    private static <T extends Base> String jsonStringHelper(T object)
        throws IOException {
        JacksonRepresentation<T> rep =
             new JacksonRepresentation<>(MediaType.APPLICATION_JSON, object);
        return rep.getText();
    }
}

and that worked fine. Why is that? Why can’t/doesn’t the compiler realize that the type of this is a type that satisfied T extends Base and do the necessary resolution?

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

    Because you can have Class1 and Class2 that both extend base, and someone could do this:

    Class1 class1 = new Class1();
    
    String result = class1.<Class2>jsonStringHelper();
    

    So while it is guaranteed that ‘this’ is a subclass of Base, there is no guarantee that ‘this’ is an instance of T.

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

Sidebar

Related Questions

I'm trying to put some anti sql injection in place in java and am
I am trying to develop my first restful service in Java and having some
I'm trying to Or some conditions in MongoDB (using the Java driver). This is
I am trying this import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream;
I'm trying to do something like this in Java: public static <T> T foo()
I'm trying to use this formula in JAVA : (-ln(1-L))/L I'm not sure how
I am trying to run this dreadfully simple command in Bash java -cp nasa-top-secret.jar
I am trying this simple tutorial from oracle : http://www.oracle.com/technetwork/java/socket-140484.html (the Example 1 ).
I'm trying to run a Java Jetty application and I keep seeing this error:
I am trying to sort a Vector list using the Java Collections frameworks. This

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.