limeandcoconut
limeandcoconut

Reputation: 408

Class implementing interface containing same interface

I can't think of a reason why I shouldn't do this but I wanted to see if I was missing something. If I have a class that implements an interface is there any reason not to have member objects of a different class implementing the same interface?

public class MemberClass implements MyInterface{
}

public class LargerClass implements MyInterface{
    private MemberClass memberClass = new MemberClass;
}

Would there be any problems that would apply to most interfaces that would not apply to marker interfaces?

Upvotes: 3

Views: 429

Answers (6)

TheLostMind
TheLostMind

Reputation: 36304

Programatically/ syntactically yes, You can do it. But from a "Design " prespective, is it right?.

Suppose you have an Animal interface and your Dog class implements it. Now, if you have 2 classes Dog and Cat implementing the same interface, you can't have a Dog/Cat inside the other class. There might be some cases in which this would work (as other's suggest, the decorator pattern..)

Upvotes: 0

AlexR
AlexR

Reputation: 115378

It is not a problem. Moreover there is a well-known design pattern named Wrapper (or Decorator) that is defined as following:

public interface Business {
    public void doSomething();
}


public class BusinessWrapper implements Business {
    private Business wrapped;

    public void doSomething() {
        // probably do something before
        wrapped.doSomething();
        // probably do something after
    }
}

Upvotes: 0

Take the example of the Decorator Pattern.

In this pattern you have a class implementing an interface which is exactly the same than the one implemented by contained object.

Upvotes: 0

Paolo
Paolo

Reputation: 22638

This is valid and is in fact a classic example of the decorator pattern, where here your LargerClass is decorating MemberClass.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502036

That's absolutely fine, and is often used for delegation, e.g. in the decorator pattern. While it's not actually using interfaces (instead using an abstract base class, Stream), BufferedStream is an example of this.

Or for example you could create a "concatenating iterable":

public class Concatenation<T> : IEnumerable<T>
{
    private readonly IEnumerable<T> first;
    private readonly IEnumerable<T> second;

    public Concatenation(IEnumerable<T> first, IEnumerable<T> second)
    {
        this.first = first;
        this.second = second;
    }

    public IEnumerator<T> GetEnumerator()
    {
        // Implicitly calls first.GetEnumerator()
        foreach (T item in first)
        {
            yield return item;
        }
        // Implicitly calls second.GetEnumerator()
        foreach (T item in second)
        {
            yield return item;
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

(Enumerable.Concat in LINQ effectively does this...)

Upvotes: 2

duffymo
duffymo

Reputation: 308918

I see no problem, just two classes that happen to implement the same interface.

I'd love to hear the design justification that made such a thing necessary, but I don't believe the compiler or runtime will complain.

Upvotes: 4

Related Questions