Clarifucation on interfaces

  • Thread starter Thread starter gemel
  • Start date Start date
G

gemel

I am trying to understand some C# code that uses interfaces. I have a
Customer class that implements an interface call ITicket.

I create an instance of the Customer class, say MyCustomer. This is
intance is passed to a method which has the signature:

Accept ( ITicket aCustomer)

Because the parameter is of type ITicket then would the parameter
aCustomer be used simple to access the methods of the interface
associated with aCustomer. Would the instance methods of Customer also
be accessible.


Thanks

John L
 
John, any object that is an instance of a class that implements a given
interface can be passed as a parameter of that interface type. So, if
your MyCustomer object is an instance of class Customer, and class
Customer implements ITicket, then yes, MyCustomer can be passed to
Accept.

Hope that helps.

Adam
 
John, any object that is an instance of a class that implements a given
interface can be passed as a parameter of that interface type. So, if
your MyCustomer object is an instance of class Customer, and class
Customer implements ITicket, then yes, MyCustomer can be passed to
Accept.

Hope that helps.

Adam

Yes Adam, that does help and thanks. My memory of Interfaces with VB6
is probably getting in the way here. In VB6 if you cast an object to
one of the interfaces that the object implements then you could use
the cast to access all the methods of that inteface (but not the
methods of the object instance)

In this case, by passing MyCustomer which is the instance object to a
parameter which is an inteface type implemented by MyCustomer then, in
effect, the receiving method is getting a 'cast' and this can be used
both to access the instance methods as well as the interface
implemented methods.

I am just replying in this way to confirm that I acctually understand
it.

Regards

John L
 
In this case, by passing MyCustomer which is the instance object to a
parameter which is an inteface type implemented by MyCustomer then, in
effect, the receiving method is getting a 'cast' and this can be used
both to access the instance methods as well as the interface
implemented methods.

This isn't exactly right. If your method takes ITicket as a paramter,
inside the method, it is just an ITicket, and the compiler will not
allow you to access the methods or properties of the MyCustomer object
unless you cast the ITicket back to MyCustomer first.

Adam
 
To clarify even further, within your Accept method, if you refere to
aCustomer (which you declared as an ITicket), you can "see" (at compile
time) only the interface methods. However, at run time, you passed a
Customer (called myCustomer) to the method. You can do that because the
Customer class implements ITicket. So, at run time, your Accept
method's aCustomer parameter is pointing to a Customer object.

The compiler will allow Accept to look at _only_ the interface methods
defined in ITicket, not the instance methods of the Customer class,
becuase all you've told it is that aCustomer implements ITicket. The
compiler doesn't know that at run time this will be a Customer object
and so will have all of Customer's instance methods.

You can use Reflection to test what kind of object it really is, and
cast it back to a Customer if you like:

public void Accept(aCustomer ITicket)
{
...
Customer cust = aCustomer as Customer;
if (cust != null)
{
... now can call object's Customer instance methods via "cust"
variable ...
}
}

By casting the aCustomer (an ITicket) back to a Customer variable
(cust), you can then get at its instance methods. However, you need to
do the cast, in effect telling the compiler, "Yes, I know that this is
a Customer object. Work with it as though it were a Customer."

It's very important here to keep in mind the distinction between what
is known at compile time and what is known at run time. The compiler is
really relying on you, the programmer, to tell it what's what. It has
_no idea_ what kind of object you're going to pass to Accept at run
time. The compiler knows only what you've told it: that whatever object
it is, at run time, it implements the ITicket interface. It's only at
run time that an actual object (which must be an instance of some
class) is passed to Accept. Only at run time do you know that this
object is really a Customer, or a SalesOrder, or a Widget, all of which
implement the ITicket interface.

Certainly you can write tests that will check (at run time) what kind
of thing aCustomer refers to:

Customer cust = aCustomer as Customer;

or

if (aCustomer is Customer) { ... }

but the compiler doesn't automagically "know" what you've passed to the
method, beyond what you've explicitly declared.
 
Back
Top