• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

Is it Interface object or class object?

IComparer myComparer = new myReverserClass();

****************************************************************
I came across the above code at
http://msdn.microsoft.com/en-us/library/system.collections.icomparer.aspx

I don't understand what it means. I was under the impression that we cannot create an object of an interface.

Please explain what does the above line of code mean? Is myComparer an object of IComparer or myReverserClass?

Thanks.
0
Ricky66
Asked:
Ricky66
  • 2
2 Solutions
 
käµfm³d 👽Commented:
You cannot create an instance of an interface. You can, however, create an instance of a class which implements a particular interface. When you assign said instance to a variable which is typed to be the interface, this means that you will only see the member of the object which are defined by the interface. In the case of IComparer this means you will only see the Compare method; you will NOT see any of the methods (excluding Compare) defined by myReverserClass. Nothing is lost from the object--you can cast the interface back to the class to see the other members:

e.g.

IComparer myComparer = new myReverserClass();
myReverserClass anotherReference = (myReverserClass)myComparer;

Open in new window


...and you could then refer to the method and properties of the myReverserClass, using the anotherReference variable in the above case.

Using a variable typed as an interface allows us to use one set of logic against many different classes which implement that interface. This allows us to write less code (leaving us with less to maintain and debug!).
0
 
käµfm³d 👽Commented:
P.S.

By this, "You cannot create an instance of an interface," I mean that you cannot do:

IComparer myComparer = new IComparer();

Open in new window

0
 
anarki_jimbelCommented:
Really kaufmed explained everything. I just want to add from a "philosophical" perspective, i.e. use some analogy.

For example, I have a pet. But there is NO just a pet, it should be a cat, a dog, a parrot, an elephant etc..
In other words, "pet" is like an interface - we cannot create just a pet but still we may have an object type of "pet". Unless I specify what specific kind of pet I have all you know is that some pet exists with the only function, e.g., "eat".

But if I want to get a new pet I create it  like:

Pet mypet = new Dog();

or

Pet mypet = new Cat().

Something like that :)
0
 
Ricky66Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now