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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:44:34+00:00 2026-06-06T21:44:34+00:00

Consider the following code: interface IFace {} abstract class Supertype {} class Subtype1 extends

  • 0

Consider the following code:

interface IFace {}

abstract class Supertype {}

class Subtype1 extends Supertype implements IFace {}
class Subtype2 extends Supertype implements IFace {}
class Subtype3 extends Supertype {}

class Foo {
    //Contains elements of Subtype1 and Subtype2
    List<IFace>     ifaceList = new ArrayList<IFace>();

    //Contains elements of Subtype1, Subtype2, and Subtype3
    List<Supertype> superList = new ArrayList<Supertype>();   

    void CopyItem() {
        superList.add( (Supertype) ifaceList.someElement() );
    }
}

Is it safe to cast an IFace element to Supertype if I know that only Subtypes will implement IFace? Is it even possible to ensure that only Subtypes will implement IFace?

I’m trying to use IFace as a marker interface to keep only certain Subtypes in the first list and allow any Subtypes in the second 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-06-06T21:44:36+00:00Added an answer on June 6, 2026 at 9:44 pm

    Is it safe to cast an IFace element to Supertype if I know that only Subtypes will implement IFace?

    Yes.

    Is it even possible to ensure that?

    If you mean “is it possible to ensure that only subclasses of Supertype implement IFace” – no. An interface can be implemented by anything.

    If you mean “is it possible to ensure that the cast will succeed” – yes, you can use instanceof before casting.

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

Sidebar

Related Questions

Consider this code: public interface Foo extends Comparable<Foo> {} public enum FooImpl implements Foo
Consider the following code : public class LIMSGrid extends ClientEventSource implements Focusable, FramingBlockWrapper {
Consider the following code. public interface IFoo { } public class Bar { public
Consider the following code: public abstract class RepositoryBase<T> where T : class { #region
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
Consider the following code : public class Main implements Vehicle, Car { public static
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
It seems JAXB can't read what it writes. Consider the following code: interface IFoo
Consider the following code: public interface A { public A another(); } public interface
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {

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.