473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Clarifucation on interfaces

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
Nov 17 '05 #1
4 975
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

Nov 17 '05 #2


On 17 Apr 2005 05:48:12 -0700, "Adam" <ad************@gmail.com>
wrote:
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
Nov 17 '05 #3
> 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

Nov 17 '05 #4
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.

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
8
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
18
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
23
by: A.Gallus | last post by:
If I declare a function pure virtual: class A { virtual void myfunc() = 0; } and I derive a class from A: class B : public A
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.