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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:49:13+00:00 2026-06-05T19:49:13+00:00

In a class, I have a container: public class MyClass implements MyClassInterface { private

  • 0

In a class, I have a container:

public class MyClass implements MyClassInterface {
  private LinkedList<OtherClass> list; //Need to use 'OtherClass' instead of 'OtherClassInterface here
  @Override
  public Iterator iterator() {
    return list.iterator; //Problem!
  }
}

The interface:

public interface MyClassInterface {
  //Is
  public Iterator iterator();
  //Should be
  public Iterator<OtherClassInterface>();
}

Then again, OtherClass also has an interface OtherClassInterface.
I want only the interfaces to be used by whom who works with the code.
The problem is that I want to use the full OtherClass inside MyClass but pass an iterator over LinkedList<OtherClassInterface> to the caller of MyClassInterface.iterator().

I could not cast the existing LinkedList<OtherClass> to LinkedList<OtherClassInterface> inside MyClass to return the desired iterator.

How to handle such a situation?

EDIT

Reason why I want this behaviour

For another developer, I want to provide two interfaces: The first gives him access to a higher data structure which contains a lower data structure which he should access by the second interface. In the implementing class of the higher interface I use the type of the lower data structure directly, not over the lower interface.

As mentioned, the other developer wants to use both interfaces. Over the higher one I want to provide an iterator that gives access to elements of the lower interface – but not to the class that implements the interface.

Additional needs

I also want the returned iterator to be “Iterable” i.e. so that I can use the “for each” construct. Is this also possible with *waxwing*s solution? If possible, I wouldn´t like to implement an own iterator – for me this seems not neccessary because I just want to give an iterator over elements of the interface instead of the implementing class.

  • 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-05T19:49:14+00:00Added an answer on June 5, 2026 at 7:49 pm

    The interface can be defined like this:

    public interface MyClassInterface {
        public LinkedList<? extends OtherClassInterface> list();
    }
    

    Then the implementation should look like this:

    @Override
    public LinkedList<OtherClass> list() {
        return list; //this is the container of type LinkedList<OtherClass>
    }
    

    This has the following advantages:

    • When calling list() by an OtherClass object you will get a LinkedList<OtherClass>
    • When calling list() by an OtherClassInterface object you will get a LinkedList<OtherClassInterface>
    • The return value of each can be used in the for-each loop
    • The iterator can be obtained by list().iterator()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine we have a class public class MyClass { private string _val; public MyClass(string
I have a class with a container (containing pointer) as a member: MyClass{ private:
All, I have a simple class. public class Container : UserControl { public bool
I have a static method on a public class. Example: public class MyClass {
Let's suppose I have this object: [Serializable] public class MyClass { public int Age
I have a C# 2.0 class with the following code: public class MyClass :
I have a class MyClass with the following constructor: public MyClass(IRepository repository, AnotherClass anObject)
i have 3 files class.myclass.php test1.php test2.php class.myclass.php contains class myclass { public $value;
I have a class that works like so: @Component public class MyClass { @Autowired
I have an Abstract class that is like: public abstract class myClass{ public abstract

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.