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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:23:59+00:00 2026-05-25T12:23:59+00:00

Consider the following interface. public interface Feed<T> { public void put( T o );

  • 0

Consider the following interface.

public interface Feed<T> {

    public void put( T o );

}

We have a straightforward Java class implementing this interface, that writes objects to a specific collection.

public class CollectionFiller<T> implements Feed<T> {

    private final Collection<T> collection;

    private CollectionFiller( Collection<T> collection ) {
        this.collection = collection;
    }

    public void put( T o ) {
        this.collection.add( o );
    }

    public static <T> CollectionFiller<T> of( Collection<T> collection ) {
        return new CollectionFiller<T>( collection );
    }

}

Now we define two dummy classes, in order to make the question concrete.

public class Super {}

public class Sub extends Super {}

Let’s say there is some method writeSubsTo( Feed<Sub> f ) and we have an instance cf of CollectionFiller<Super>. Since cf only adds objects to its collection of “Supers”, it would be safe to pass it to writeSubsTo. The compiler won’t allow it however. (The reason for this behavior is clear to me.)

I wanted to write a convenience adapter that wraps around a CollectionFiller of type X and poses as a Feed of a specific subtype of type X. I tried (among many other things) the following, but the compiler gives me trouble.

class SubFiller<T1, T2 extends T1> implements Feed<T2> {

    private final CollectionFiller<T1> collectionFiller;

    SubFiller( CollectionFiller<T1> collectionFiller ) {
        this.collectionFiller = collectionFiller;
    }

    public void put( T2 o ) {
        this.collectionFiller.put( o );
    }

    public static <T1, T2 extends T1> SubFiller<T1, T2> of( CollectionFiller<T1> c ) {
        return new SubFiller<T1, T2>( c );
    }

}

Although there is nothing wrong with this class, the compiler won’t accept both of the last two statments in the following code fragment.

CollectionFiller<Super> cf = CollectionFiller.of( new HashSet<Super>() );
SubFiller<Super, Sub> sf = SubFiller.of( cf );
writeSubsTo( SubFiller.of( cf ) );

Can anyone think of a nice way of solving this problem? I wouldn’t mind if the adapter contained cumbersome code, as long as using it is not too verbose (hence the static factory methods). Of course, any other solution (not using an adapter) is also fine (again, as long as using it isn’t too verbose).

  • 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-25T12:23:59+00:00Added an answer on May 25, 2026 at 12:23 pm

    If you use writeSubsTo(Feed<? super Sub> f), then you can pass in a Feed<Super>. Then you can do away with your filler classes altogether. Yay for contravariance! 😛

    (There’s also a covariant version, ? extends X, for cases where you’re getting values out rather than putting values in.)

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

Sidebar

Related Questions

Consider I have the following interface: public interface A { public void b(); }
Consider the following example: public interface ITask { void Execute(); } public class LoggingTaskRunner
Consider the following interface in Java: public interface I { public final String KEY
Please consider the following three .NET types: I have an interface, an abstract class,
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following classes: public interface IView { /// <summary> /// Ignore this (its
Consider the following code. public interface IFoo { } public class Bar { public
Consider the following interface: public interface IMyCallback { void SomeEvent(int someArg); } which is
Consider the following: public interface ICleanCoder { void DoSomeCoding(object task); } public interface ICleanCoder<T>
Consider the following example: public class Sandbox { public interface Listener<T extends JComponent> {

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.