4

EDIT: Sorry if this seem a little obvious/simple, I tried finding an answer but I'm not sure how to word it correctly

I've been studying a little C# and I'm having some trouble with getting my head around instantiated new objects, where the constructor and the class type are different. So a regular object would be instantiated by:

Object obj = new Object();

That's pretty obvious, but since I've been studying interfaces I've come across some syntax like follows:

interface ISaveable {
    string Save();
}

public class Catalog : ISaveable {
    string Save() {
        return "Catalog Save";
    }

    string ISaveable.Save(){
        return "ISaveable Save";
    }
}

And then the tutorial went on to do something along the lines of this:

Catalog c1 = new Catalog();

So I know here a new instance of the catalog class is being instantiated, however I can't for the life of me figure out this next line:

ISaveable c2 = new Catalog();

Now the actual code itself is no problem (I understand explicit and implicit implementation of interfaces etc.), but it's just the part about the actual instantiation of the above object c2. Why is the class type (ISaveable) different to the constructor ( new Catalog() )?

Any explanation will be very appreciated!

  • what you want to say here : **Why is the class type (ISaveable) different to the constructor ( new Catalog() )?** – Ehsan Sajjad Sep 08 '15 at 13:05
  • Your usage of the term "constructor" and "explicit interface implementation" add noise. It's just about object types and references. – CodeCaster Sep 08 '15 at 13:11
  • Catalog class implements ISaveable interface. So, you can declare c2 like ISaveable an then instantiated as an object of type Catalog. If you have another class called Catalog2, you can do this: ISaveable c2 = new Catalog2(); – Jack1987 Sep 08 '15 at 13:11
  • The "class type" is not different to the constructor, the declared type of the variable is different to the runtime type of the object being instantiated. c2 is still a `Catalog` it is just declared as as an ISaveable. it is no different to doing something like `object obj = new SomeOtherType()` – Ben Robinson Sep 08 '15 at 13:14
  • @BenRobinson That's one thing I don't quite understand though, since I've started learning OOP I've always declared objects using the same variable type and the runtime type (like in my first code block above); I've never done it where the runtime type of the object has been different to the declared type of the variable =/ – harveysingh Sep 08 '15 at 13:16
  • @harveysingh Its an important new thing to learn then. The point in the example is because the declared type is `ISaveable` then any calls to `Save` will call `ISaveable.Save()`. It is also important for virtual methods as well. You might want to try reading this and other articles on ploymorphism https://msdn.microsoft.com/en-us/library/ms173152.aspx – Ben Robinson Sep 08 '15 at 13:25

2 Answers2

1

To my understanding, the actual type of the objects created by

Catalog c1 = new Catalog();

and

ISaveable c2 = new Catalog();

is the same, namely Catalog (as the same constructor is called), however the type of the referece is different; a call of Save on c1 and c2 will result in calls to the different implementations.

Codor
  • 17,447
  • 9
  • 29
  • 56
0

Now the actual code itself is no problem (I understand explicit and implicit implementation of interfaces etc.), but it's just the part about the actual instantiation of the above object c2. Why is the class type (ISaveable) different to the constructor ( new Catalog() )?

ISaveable c2 = new Catalog();

In the above code, you're:

  • creating a new Catalog object
  • using a variable of type ISaveable to reference it

So basically you have a concrete implementation of Catalog, but you'll have to use it as ISaveable.

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • So if I try and call c2.Save() will it not return "Catalog Save", but instead return "ISaveable Save"? – harveysingh Sep 08 '15 at 13:13
  • 2
    @harveysingh Yeah, that's basically the difference between _explicit and implicit interface implementation_. This is a different question, see http://stackoverflow.com/a/143423/870604 – ken2k Sep 08 '15 at 13:16