Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get an error instantiating an interface?

I have a class and an interface, and when I try to instantiate the interface, I get an error:

Cannot create an instance of the abstract class or interface

My code is below:

namespace MyNamespace
{
    public interface IUser
    {
        int Property1 { get; set; }
        string Property2 { get; set; }
        string Property3 { get; set; }
        void GetUser();
    }

    public class User : IUser
    {
        public int Property1 { get; set; }
        public string Property2 { get; set; }
        public string Property3 { get; set; }

        public void GetUser()
        {
           //some logic here...... 
        }

    }
}

When I try to instantiate IUser user = new IUser(); I get an error:

Cannot create an instance of the abstract class or interface

What am I doing wrong here?

like image 881
Alex Avatar asked Jul 07 '11 13:07

Alex


People also ask

Why can't you instantiate an interface?

You can't instantiate an interface or an abstract class because it would defy the object oriented model. Interfaces represent contracts - the promise that the implementer of an interface will be able to do all these things, fulfill the contract.

How can we instantiate an interface?

An interface can't be instantiated directly. Its members are implemented by any class or struct that implements the interface. A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.

Is it legal to instantiate an interface object in Java?

You can never instantiate an interface in java. You can, however, refer to an object that implements an interface by the type of the interface.

Can I instantiate interface C#?

Interface can not be directly instantiated. We can create an instance of a class that implements the interface, then assign that instance to a variable of the interface type.


2 Answers

The error message seems self-explanatory. You can't instantiate an instance of an interface, and you've declared IUser as an interface. (The same rule applies to abstract classes.) The whole point of an interface is that it doesn't do anything—there is no implementation provided for its methods.

However, you can instantiate an instance of a class that implements that interface (provides an implementation for its methods), which in your case is the User class.

Thus, your code needs to look like this:

IUser user = new User(); 

This instantiates an instance of the User class (which provides the implementation), and assigns it to an object variable for the interface type (IUser, which provides the interface, the way in which you as the programmer can interact with the object).

Of course, you could also write:

User user = new User(); 

which creates an instance of the User class and assigns it to an object variable of the same type, but that sort of defeats the purpose of a defining a separate interface in the first place.

like image 74
Cody Gray Avatar answered Sep 20 '22 01:09

Cody Gray


Imagine if one went into a store and asked for a device with a power switch. You didn't say whether you wanted a copier, television, vacuum cleaner, desk lamp, waffle maker, or anything. You asked for a device with a power switch. Would you expect the clerk to offer you something that could only be described as "a device with a power switch"?

A typical interface would be analogous to the description "a device with a power switch". Knowing that a piece of equipment is " a device with a power switch" would allow one to do some operations with it (i.e. turn it on and off), and one might plausibly want a list of e.g. "devices with power switches that will need to be turned off at the end of the day", without the devices having to share any characteristic beyond having a power switch, but such situations generally only apply when applying some common operation to devices that were created for some more specific purpose. When creating something from scratch, one would more likely wand a "copier", "television", "vacuum cleaner", or other particular type of device, than some random "device with a power switch".

There are some circumstances where one may want a vaguely-defined object, and really not care about what exactly it is. "Give me your cheapest device that can boil water". It would be nice if one could specify that when someone asks for an arbitrary object with "water boiling" ability, they should be offered an Acme 359 Electric Teakettle, and indeed when using classes it's possible to do that. Note, however, that someone who asks for a "device to boil water" would not be given a "device to boil water", but an "Acme 359 Electric Teakettle".

like image 24
supercat Avatar answered Sep 22 '22 01:09

supercat