1

I would like to make the return type of my method an interface rather than a class for similar reasons stated in c# List or IList, however I am having trouble figuring out how to initialize the interface to return it. I cannot use new IA() and (IA) new A() does not work as I cannot cast the result to B.

interface IA{}   
class A: IA{}
class B: IA{}

class UseIA
{
  public IA DesiredMethod()
  {
    return ???;// new IA()
  }
  public A UndesiredMethod()
  {
    return new A();
  }
}
  • Although you need a concretion to do something, there is nothing to stop you referring to concretions using their interface e.g. IMyInterface r = new MyClass(); N.B. this is sometimes written as IMyInterface r = new MyClass() as IMyInterface; but the as statement is pure cruft here and has no effect (see the CIL if in doubt). – Robbie Dee Aug 11 '14 at 16:35
3

An implementation of an interface is the realization of that interface. Realization is being implemented within the class. Just return an instance of the class (A or B) which realizes the return-type interface.

interface IA{}   
class A: IA{}

class UseIA
{
  public IA DesiredMethod()
  {
    return new A();
  }
}
  • The program sees the output of DesiredMethod as a boxed object of type A. I dislike that as this means that the returned object is not just an instance of IA but it is also an A type. But as far as I can see, this is the only possible answer. – Strategy Thinker Aug 10 '14 at 21:30
  • @StrategyThinker Every interface has an implementation type. There's no such thing as an object of type IA that isn't also of some other type X. – Doval Aug 10 '14 at 21:43
  • 2
    @StrategyThinker You are getting too hung up on the implementation details of the object being returned. No matter what, there cannot be an instance of IA returned since it is an interface that cannot be instantiated directly. What you are looking for is an object that answers to the same methods as IA, abstracting away the implementation details. If you are worried about casting to implementing types, you are inappropriately breaking the abstraction, and should rethink your design. – cbojar Aug 11 '14 at 5:04
  • Thanks, these comments together with what Kaan wrote answer my question. Before I accept the answer, should I edit Kaan's answer so that it includes what is said in the comments? – Strategy Thinker Aug 11 '14 at 21:04
2

The point of declaring an interface as the return type is not to actually return something with only an interface type. You can't, since interfaces can't be instantiated. The point is to make the contract of your method more explicit: "I'm returning something with a doA() method, but not necessarily a classic StandardA object. I might be switching to a SecretSuperDuperA object some day, and you wouldn't know the difference. All you know is that whatever I return will have the doA() method you need."

In other words, it allows the implementation writer more latitude how to do the job of the method best, while still ensuring that the caller actually gets the specific bit of functionality that they need.

0

Just a small expansion on @Kaan's post. This is to point out polymorphism, since the O.P. has two classes implementing one interface.

interface IA { }
class A : IA { }
class B : IA { }

class UseIA
{
    public IA DesiredMethod()
    {
        return new A;   // let's say, for the purposes of the exercise, that class A is the default implementation of IA
    }

    public IA DesiredMethod(String whichImplementationOfIA)   // overloaded method
    {
        switch (whichImplementationOfIA)
        {
            case "GiveMeA": return new A();
            case "GiveMeB": return new B();
            default: 
                throw new ArgumentException();     // or, alternatively, return A, because it's a default implementation in this exercise
        }
    }
}

By the way, the second scenario (where an argument specifies which class will be created) is reminiscent of Class Factory patterns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.