Is it Interface object or class object?

Posted on 2012-09-19
Medium Priority
Last Modified: 2012-09-20
IComparer myComparer = new myReverserClass();

I came across the above code at

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?

Question by:Ricky66
  • 2
LVL 75

Accepted Solution

käµfm³d   👽 earned 1600 total points
ID: 38415502
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:


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!).
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38415514

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

IComparer myComparer = new IComparer();

Open in new window

LVL 30

Assisted Solution

anarki_jimbel earned 400 total points
ID: 38416267
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();


Pet mypet = new Cat().

Something like that :)

Author Closing Comment

ID: 38420260

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question