JTK
JTK

Reputation: 1519

Collection of type interface C#

I've just completed a lab for college, the lab asked me to create an interface IHasVolume with a Volume method and then to create a class, Sphere, that implemented the interface and by default it's method. I then had to create a Test and call the method in my class polymorphically via a reference to the interface.

snippet of my code:

IHasVolume i = new Sphere { Radius = 2 };

I also had to make a collection of Spheres and call the various methods

IHasVolume[] collection = { new Sphere(4), new Sphere(6), new Sphere(9), new Sphere(10), };

        foreach(IHasVolume s in collection)
        {
            Console.WriteLine("Volume: " + s.Volume());
            Console.WriteLine("Radius: " + s.ToString());
        } 

Now I'm not sure I was actully been asked to make the array of type IHasVolume, But I did and it worked, I'm confused as to why it worked though, Sphere is not a subclass of IHasVolume, or is it?

Could someone explain this to me please?

Upvotes: 2

Views: 740

Answers (5)

lintmouse
lintmouse

Reputation: 5119

It is not a subclass, but the behavior would be similar if you had a base class called HasVolume instead of an interface. But with an important difference. Your HasVolume base class would require its own implementation of the methods it defines.

This is not the case with an interface. You defer to the implementer to define the operation, in this case Sphere. This makes sense when you don't need a base implementation.

For your example, you can get the volumes from your array and total them without caring what type of container they are - sphere, cylinder, etc. They can calculate their own volumes accordingly, but your method doesn't need to be concerned with how exactly they do that. You just want to know how much beer they can all hold :).

Upvotes: 1

Saman Najafi
Saman Najafi

Reputation: 1

Interfaces are the ways to achieve late-binding polymorphic so when ever a class implement the interface it assure all interface member can be used and you can call it. a classic example for interfaces are ICar which represents the car then the models of different cars will implement the ICar interface and so each model can have their implementation.

Upvotes: -1

Rohit
Rohit

Reputation: 10246

If you do something like

IBlah blah = new BlahImpl();

What you can do as you have in this example is to instantiate a concrete class but assign the resulting object to an interface.

the effect is same as if you had instantiated a class without interface.

Upvotes: 0

David Shnayder
David Shnayder

Reputation: 333

You created an array of type IHasVolume which means that it's items can be not only a Sphere but any class that inherits from this interface...

in your loop you also specified it as a type of the interface which means any object that inherits from this interface will be acceptable...

Upvotes: 0

Avner Shahar-Kashtan
Avner Shahar-Kashtan

Reputation: 14700

The relationship between a subclass and a superclass is similar to that of an implementing class and an interface. You can think of an interface as a pure abstract base class that contains no logic, only public method (and property) signatures.

So yes, your Sphere has an is-a relationship with IHasVolume, and will behave the same as if you had an abstract HasVolume class that was inherited.

Upvotes: 1

Related Questions