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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:47:43+00:00 2026-06-06T06:47:43+00:00

Consider the following scenario: /** * A sample interface. */ public interface MyInterface {

  • 0

Consider the following scenario:

/**
 * A sample interface.
 */
public interface MyInterface
{
}

/**
 * First sample implementation of the above interface.
 */
public class MyClass1 implements MyInterface
{
    public String toString()
    {
        return "[ My Class 1 ]";
    }
}


/**
 * Second sample implementation of the above interface.
 */
public class MyClass2 implements MyInterface
{
    public String toString()
    {
        return "[ My Class 2 ]";
    }
}


import java.util.Collection;

/**
 * A service interface that declares a generic method
 * returning a collection of subtype the interface defined above.
 */
public interface MyService
{
    public <T> extends MyInterface<Collection<T>> myMethod();

}

import java.util.Arrays;
import java.util.Collection;

/**
 * The implementation of the service interface 
 * that returns the generic type. 
 */
public class MyServiceImpl implements MyService
{

    @Override
    public Collection<MyInterface> myMethod()
    {
        return Arrays.asList(new MyClass1(), new MyClass2());
    }

}

import java.util.Collection;

/**
 * Simple main class to drive the point 
 * I would like raise in the query below.
 */
public class MyMain
{
    public static void main(String[] args)
    {
        MyService service = new MyServiceImpl();
        Collection<MyClass1> list = service.myMethod();
        // This works at runtime.
        System.out.println(list);
        for (MyClass1 obj : list)
        {
            // This throws ClassCastException at runtime.
            System.out.println(obj);
        }
    }
}

In the above code, how can Java generics implementation allow the MyServiceImpl’s implementation to return a generic class when the MyService declaration talks about a specific subtype of a given type?

  • 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-06T06:47:44+00:00Added an answer on June 6, 2026 at 6:47 am

    If I add the correct generic types

    public class MyMain {
        public static void main(String[] args) {
            MyService service = new MyServiceImpl();
            Collection<MyInterface> list = service.myMethod();
            // This works at runtime.
            System.out.println(list);
            for (MyInterface obj : list) {
                // This doesn't throw a ClassCastException
                System.out.println(obj);
            }
        }
    }
    

    I get

    [[ My Class 1 ], [ My Class 2 ]]
    [ My Class 1 ]
    [ My Class 2 ]
    

    I don’t see how you can get your example to compile without warning and trigger a ClassCastException.

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

Sidebar

Related Questions

Original Question Consider the following scenario: public abstract class Foo { public string Name
Consider the following scenario: public class Entity1 { virtual public Int32 ID { get;
consider the following code scenario: <?php //widgetfactory.class.php // define a class class WidgetFactory {
Consider a following chunk of service: public class ProductService : IProductService { private IProductRepository
Consider the following scenario: class A<T> {} class B extends A<Integer> {} How can
Consider the following class: public class MyIntSet { private List<int> _list = new List<int>();
Please consider the following scenario. I have a form with a property: class MyForm
consider following scenario input string = WIPR.NS i have to replace this with WIPR2.NS
Consider the following scenario We have a simple database that involves two entities: user
Consider following scenario: I have RESTful URL /articles that returns list of articles user

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.