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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:46:51+00:00 2026-05-11T17:46:51+00:00

[Update]: my initial example doesn’t reflect my problem. Updated the sample, hopfully it is

  • 0

[Update]: my initial example doesn’t reflect my problem. Updated the sample, hopfully it is better now.

Java Generic and overloading don’t play well together.

public interface Request<E> {
  E getValue();
}

I have a generic parameterized Request interface as above, and I would like write a DataStore class to save the payload request carries. The save logic is different for different payload type.

What I really want is something like

public interface DataStore {
  void save(Request<E1> r);
  void save(Request<E2> r);
}

But this is of course illegal.

If I want to preserve type safety, DataStore must declare different methods for each type I want to handle, e.g

public interface DataStore {
  // here E1 is the actual type I want to handle, not a generic parameter
  public void saveE1(Request<E1> e); 

  public void saveE2(Request<E2> e);
}

Or sacrifices type safety

public class DataStore {
  public void save(Request e) {
    Object value = e.getValue();
    if (value instanceof ReqE1Impl) {
      // do something
    } else if (value instanceof ReqE2Impl) {
      // do something else
    }
  }
}

Both ways suck! Is there any better way?

  • 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-11T17:46:52+00:00Added an answer on May 11, 2026 at 5:46 pm

    The better way to do this would involve the object knowing how to save itself. If you still want to use a DataStore, you could do something like this:

    interface YourInterface {
      void save();
    }
    
    interface Request<E extends YourInterface> { 
      E getValue(); 
    } 
    
    class DataStore<E extends YourInterface> {
      public void save(Request<E> r) {
        r.getValue().save();
      }
    }
    

    You just have to ensure that your objects then each implement that interface and viola, you have Generics and polymorphism playing nice with each other.

    NOTE: The way your (revised) question is written, you are not using polymorphism. You are using overloading. Method overloading is decided at compile time, while method polymorphism is decided at runtime. This is part of why you are running into these difficulties.

    If you really do not want the above kind of design where the objects do all the work of saving themselves, then perhaps you can do something like this (as a very weak example):

    interface YourInterface {
      String serialize();
    }
    
    interface Request<E extends YourInterface> { 
      E getValue(); 
    } 
    
    class DataStore<E extends YourInterface> {
      public void save(Request<E> r) {
        String value = r.getValue().serialize();
        // Now do something with value to save to a datastore
      }
    }
    

    Above is a weak example, but the main idea is that the objects know how to do the part of serialization that differs between objects, and the DataStore can then do the work that is common to all of your objects.

    And yes, you are right (with the wrong terminology) — generics don’t play nice with overloading. But they do play nice with polymorphism. Polymorphism is usually a better instrument than overloading anyway.

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

Sidebar

Ask A Question

Stats

  • Questions 107k
  • Answers 107k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer My problem was in System.Web.Extensions 3.5, now I changed to… May 11, 2026 at 9:02 pm
  • Editorial Team
    Editorial Team added an answer To load the values into the table, use: CREATE TABLE… May 11, 2026 at 9:02 pm
  • Editorial Team
    Editorial Team added an answer The following code works. I've made no attemps to optimize… May 11, 2026 at 9:02 pm

Related Questions

I have a data set whose elements are displayed as rows in a DataGrid.
I have implemented what I thought was a pretty decent representation of MVC in
The GString concept in Groovy is pretty powerful (see http://groovy.codehaus.org/Strings+and+GString ). GStrings let you
I'm working on a Rails web application, and it's currently being used by some
I would like to sort an array in ascending order using C/C++ . The

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.