Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a doubt regarding the interface.See the below code

Class sample:IApplication
    Test obj = new Test();
Class Test
    Public void Init(IApplication app)


It is possible to accept as interface object even we are passing this pointer of sample.Please explain how?

Thanks in advance
Updated 24-Nov-11 23:08pm

1 solution

This is all about assignment compatibility of types related through inheritance. It does not matter if a base type is an interface or a class, it does not matter if the base is direct or indirect base.

Given a type A is a base type for a type B:

class B : A {/* ... */}


A a = //...
B b = //...
a = b; //you can do it: b is wider than a
//b = a; //you cannot do it

It should be clear why: the compile-time type of b allows to use some members additional to the type A. If second assignment was possible, the attempt to access such member of b would be a disaster, because in actual run-time type of b which actually would be A this member does not exist. The opposite situation (first assignment) does not create any problems.

The conclusion should be apparent to you by now.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900