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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:43:46+00:00 2026-05-27T10:43:46+00:00

This question came to mind while I was writing a class that iterates over

  • 0

This question came to mind while I was writing a class that iterates over a list, with methods next() and previous() that will continuously loop (e.g. if at the last object, return it, and then reset index to 0)

In the constructor I was pondering adding a boolean variable, which if true would just act like a regular iterator with only next() methods and no looping. In this case, having the method previous() would make no sense. So I’m curious, is it possible to hide the previous() method in this case. Is it possible to achieve this somehow in Java or C#?.

What about other languages?

  • 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-27T10:43:47+00:00Added an answer on May 27, 2026 at 10:43 am

    C#

    It is possible by making the two methods part of two different interfaces, and casting the object to one of the two interfaces. For example:

    interface ILoopingIterator
    {
        void Next();
        void Previous();
    }
    
    interface INonLoopingIterator
    {
        void Next();
    }
    
    class PlaysItBothWays : ILoopingIterator, INonLoopingIterator
    {
        void ILoopingIterator.Next()
        {
             this.NextCore();
        }
    
        void ILoopingIterator.Previous()
        {
             // since this code will never be shared anyway, put it here
        }
    
        void INonLoopingIterator.Next()
        {
             this.NextCore();
        }
    
        private void NextCore()
        {
            // do stuff here; this method only exists so that code can be shared
        }
    }
    

    Note that I have made the class implement both interfaces explicitly; this way, users of instances are forced to select which “mode” they want to use the class in. You could implement only one interface explicitly instead (providing a “default” mode that can be changed).

    and now:

    var looping = (ILoopingIterator) new PlaysItBothWays(); // selects mode A
    var nonLooping = (INonLoopingIterator) new PlaysItBothWays(); // selects mode B
    

    Of course this does not stop anyone from casting the instance to the “other” interface if they want to, but if the programmer wants to subvert their own code they can also use reflection which allows much more than that.

    Java

    In Java, the above is not possible. You can come close by having the class expose methods that return instances of one of the two interfaces, and using the returned value. Of course then the object is really a factory and not a service provider, so that’s feels like cheating on the problem.

    class PlaysItBothWays
    {
        public ILoopingIterator asLooping() { return /* something */ }
        public INonLoopingIterator asNonLooping() { return /* something else */ }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While writing a simple server-client application, this question came in my mind. When someone
This question came into my mind while generating sample data for a SO-answer. I
This question came to my mind while I am reading the post Why doesn't
This question came to my mind while working on 2 projects in AI and
So here I am tonight with this question that came up into my mind
This question came to my mind when I learned C++ with a background of
This question came today in the manipulatr mailing list. http://groups.google.com/group/manipulatr/browse_thread/thread/fbab76945f7cba3f I am rephrasing. Given
This question came about because the cells gem specifies template directories using File.join('app','cells'). That
This question came to my mind quite a few times. Let my explain my
I was working on shared memory and this question came in my mind so

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.