Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the Concrete Type behind an Interface instance

People also ask

Can something be an instance of an interface?

Yes it is correct. you can do it with an inner class.

What is an instance of an interface?

As you might guess, this instanceof interface behavior works many levels deep, so if you have a Java class that implements an interface , and that interface implements another interface, the object you create from the actual class will be an instance of that class, the interface it implements, and the interface above ...

Can you have an instance of an interface Java?

You cannot create instances of a Java interface by itself. You must always create an instance of some class that implements the interface, and reference that instance as an instance of the interface.

Can you create an instance of an interface using its constructor?

You can't Create Instance of Interface,But you can create reference to the Interface by using Child class reference objectlike ParentInterface objParent=new ChildClassName(); objParent.


Type objectType = myObject.GetType();

Should still give you the concrete type, according to your example.


What are you doing is really bed design but you don't have to use reflection you can check it like this

void PerformAction(object myObject)
{
    B objectType = myObject as B;   // Here is where I get typeof(IA)
    if ( objectType != null )
    {
        //use objectType.b
    }
    else
    {
       //Same with A 
    }
    // snip - other actions on objectType
}

I have to agree about the bad design. If you have an interface, it should be because you need to utilize some common functionality without caring what the concrete implementation is. Given you're example, it sounds like the PerformAction method should actually be a part of the interface:

public interface IA
{
    int a { get; set; }
    void PerformAction();
}

public class B: IA
{
    public int a { get; set; }
    public int b { get; set; }

    public void PerformAction()
    {
        // perform action specific to B
    }
}

public class C : IA
{
    public int a { get; set; }
    public int c { get; set; }

    public void PerformAction()
    {
        // perform action specific to C
    }
}

void PerformActionOn(IA instance)
{
    if (instance == null) throw new ArgumentNullException("instance");

    instance.PerformAction();

    // Do some other common work...
}


B b = new B();
C c = new C();

PerformActionOn(b);
PerformActionOn(c);

You can never have instances of an Interface. So determining whether you are dealing with an interface or a concrete type is not possible since you will always be dealing with a concrete type. So I am not sure you question makes any sense. What exactly are you trying to do and why?


Maybe you are looking for the is operator

void PerformAction(object myObject)
{
    if (myObject is B)
    {
        B myBObject = myObject as B;
        myBObject.b = 1;
    }

    if (myObject is C)
    {
        C myCObject = myObject as C;
        myCObject.c = 1;
    }

    // snip - other actions on objectType
}