Is it Interface object or class object?

Posted on 2012-09-19
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
    LVL 74

    Accepted Solution

    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 74

    Expert Comment

    by:käµfm³d 👽

    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 29

    Assisted Solution

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now