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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:54:10+00:00 2026-05-13T17:54:10+00:00

I ran into some code today that I found questionable. Here’s a simplified example

  • 0

I ran into some code today that I found questionable. Here’s a simplified example (not realistic).

public interface IListable {
    //returns first n items from list
    public ArrayList getFirstNThings(int n);

    //returns last n items from list
    public ArrayList getLastNThings(int n);
}

Then there’s an implementor like so:

public GroceryList implements IListable {
    private ArrayList<GroceryItem> groceries;

    public GroceryList() {
        this.groceries = new ArrayList<GroceryItem>();
    }

    public ArrayList<GroceryItem> getFirstNThings(int n) {
        ArrayList<GroceryItem> firstNThings = new ArrayList<GroceryItem>();
        for (int i=0; i < n; i++) {
            firstNThings.add(this.groceries.get(i));
        }
        return firstNThings
     }

     public ArrayList<GroceryItem> getLastNThings(int n) {
         ArrayList<GroceryItem> lastNThings = new ArrayList<GroceryItem>();
         for (int i=this.groceries.size(); i < this.groceries.size()-n; i--) {
           lastNThings.add(this.groceries.get(i-1);
         }
         return lastNThings;
      }
}

Ignore any implementation problems you may find in that (I found a few too). What I’m getting at is that the interface does not use any generic type parameter for ArrayList (i.e ArrayList<?>), but the implementor of the interface’s method does (i.e. ArrayList<GroceryList>). Other implementors may return ArrayLists with any other type parameters, no?

So my questions: Is this a problem? Should I refactor anything? Is it worth it? What’s the advantage? What kind of problems can I run into if I have a method defined in an interface whose return type is a raw type, but the actual implementors of the method return various parameterized types?

  • 1 1 Answer
  • 3 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-13T17:54:10+00:00Added an answer on May 13, 2026 at 5:54 pm

    if both methods of IListable always return the same type, use this instead:

    public interface IListable<T> {
      //returns first n items from list
      public ArrayList<T> getFirstNThings(int n);
    
      //returns last n items from list
      public ArrayList<T> getLastNThings(int n);
    }
    

    if this isn’t an option, try using ? instead. While it’s basically the same, it avoids ugly warnings.

    public interface IListable {
      //returns first n items from list
      public ArrayList<?> getFirstNThings(int n);
    
      //returns last n items from list
      public ArrayList<?> getLastNThings(int n);
    }
    

    Generally, it’s not a problem to use a more specific return type in an implementation than in a super-type or interface. If you’re dealing with IListable, you need to handle any object type in the returned list. If you’re dealing with GroceryList, you expect only GroceryItems. That’s not only true for genric type arguments of return types, but for the return type itself as well. So if an interface specifies List<Foo> get(), is okay to implement it as ArrayList<Foo> get().

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

Sidebar

Related Questions

Today, I ran into some code that goes like this. namespace Foo { public
I ran into some Java code at work today : public interface A {
I ran into some singleton code today in our codebase and I wasn't sure
This is a very simplified version of some code I just ran into at
I've recently ran into some code that checks Page.IsCallBack but I wasn't sure how
When trying to use the code presented here link text I ran into some
So I ran into a situation today where some production code was failing precisely
I ran into a weird situation today doing some one-time sql code. This nested
I just ran into some code that overuse semicolons, or use semicolon for different
Today I ran into some weird behaviour with a HashSet's iterator. In the code

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.