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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:24:17+00:00 2026-05-26T10:24:17+00:00

I am creating several functions in which I want to return the interface instead

  • 0

I am creating several functions in which I want to return the interface instead of the implementation, I.E. List instead of ArrayList . My method signature and a brief implementation follows:

public List<MyAwesomeComposedObject> convert(Collection<MyAwesomeObject> awesomeObjects>)
{
    List<MyAwesomeComposedObject> composedObjects = new ArrayList<MyAwesomeComposedObject>();
    for(MyAwesomeObject awesomeObject : awesomeObjects)
    {
        MyAwesomeComposedObject composedObject = new MyAwesomeComposedObject(awesomeObject);
        composedObjects.add(composedObject);
    }

    List<MyAwesomeComposedObject> composedObjectList = Collections.checkedList<composedObjects, MyAwesomeComposedObject.class);
    return composedObjectList;
}

My question is, is this an antipattern of some sort? I want to guarantee that the invoker of this method is getting the interface instead of an implementation. I also do not believe this to be a case of overengineering. If this is not the correct way to return an interface, in this scenario I am open to the correct implementation.

Attached is a small program that results in an exception:

public static void main(String[] args)
{
    Vector v = (Vector) c();
}

static List<Object> c()
{
    List<Object> l = new ArrayList<Object>();
    l.add(new Object());
    List<Object> o = Collections.checkedList(l, Object.class);
    return o;
}

The javadoc is here: checked list

  • 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-26T10:24:18+00:00Added an answer on May 26, 2026 at 10:24 am

    For your revised question: There is no way to prevent the caller from attempting to cast to whatever they want. If it is an inappropriate cast they will get the exception. This is the very reason why casting from an interface to a concrete class is strongly discouraged.

    If you are really worried about this, consider returning an ArrayList instead of a List. That should discourage casting since they are getting a concrete type. Please note that I do not endorse this, it is just an option.

    I want to guarantee that the invoker of this method is getting the interface instead of an implementation

    This is not valid. You are returning a List where the declared type of the elements is an interface, however each element must be SOME instantiation. All a checked collection does is prevent the addition of elements of the incorrect type. There is nothing that prevents the user from casting back to the implementation type.

    If you are attempting to ensure that the user gets List instead of ArrayList (my assumption here because I don’t see an interface for you Awesome class), this again is flawed because the user could still cast the List to an ArrayList although this would be a bad idea since it risks a ClassCastException.

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

Sidebar

Related Questions

I've created several helper functions which I use when creating templates for Wordpress. An
In my program, I am creating several threads in the main() method. The last
I'm creating a gem which has several scripts in the bin directory the utility
we've been creating several half-transparent 3D cubes in a scene by OpenGL which displays
I am creating several image dynamically using the following code: function refresh_gallery(galleryidentifier, albumid) {
using c# (asp.net) i'm programmatically creating several drop down lists (random number). the user
I'm creating a WPF application where several ListView selections are made in a row
While creating JavaScript with ASP.NET MVC I noticed several scope warnings and realized that
I'm creating an API for a module and after I created several methods inside
I've seen several questions/answers related to entirely creating a button in code, but not

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.