473,372 Members | 1,457 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,372 software developers and data experts.

Interfaces and Inheritance

Hi all.

I have bean studying the use of Interfaces, and from what I understand
if you create a class that inherits from an interface, that class must
poses a member that represents each member of the derived interface.

Well this all seams sensible because otherwise it would defy the
principles behind inheritance,

I was studying the class “System.Collections.Generic.List<T>”.
This class states that it derives from the interface
“System.Collections.Icollection”
Therefore I would assume that it would possess all the members that
the interface contains.

This is where I become very confused.
“System.Collections.Generic.List<T>” douse not poses the members:

IsSynchronized
SyncRoot

Trying to find an answerer to this I ran the code

<Code>
System.Collections.ICollection i = new
System.Collections.Generic.List<string>();
System.Windows.Forms.MessageBox.Show(i.IsSynchroni zed.ToString());
</Code>

I found that it ran successfully and displayed the value “False”.
This states to me that the class douse in fact poses the member
“IsSynchronized” but fore some reason douse not give you access to it
at design time.

Could anybody please explain this to me?

Thanks Jay Dee
Jun 27 '08 #1
4 1087
Jay Dee wrote:
I have bean studying the use of Interfaces, and from what I understand
if you create a class that inherits from an interface, that class must
poses a member that represents each member of the derived interface.
Technically, a class does not inherit from an interface, it implements it.
It's useful to make the distinction because the mechanisms are slightly
different.
Well this all seams sensible because otherwise it would defy the
principles behind inheritance,

I was studying the class “System.Collections.Generic.List<T>”.
This class states that it derives from the interface
“System.Collections.Icollection”
Therefore I would assume that it would possess all the members that
the interface contains.
It does.
This is where I become very confused.
“System.Collections.Generic.List<T>” douse not poses the members:

IsSynchronized
SyncRoot
Yes, it does! It implements the interface explicitly. This means you can
only access these members from a reference of type ICollection...
Trying to find an answerer to this I ran the code

<Code>
System.Collections.ICollection i = new
System.Collections.Generic.List<string>();
System.Windows.Forms.MessageBox.Show(i.IsSynchroni zed.ToString());
</Code>
....as you demonstrate here.
Could anybody please explain this to me?
It's called "explicit interface implementation". The MSDN explains it here:
http://msdn.microsoft.com/library/ms173157.aspx

The idea is to use explicit interface implementations when regular clients
are not expected to be interested in the members; only clients accessing the
class through the interface will be.

A clearer example is ICollection.IsReadOnly. For most collections, you do
not want to expose this property because the collection will *always* be
read-only or read-write, as part of its type. In this case it makes sense to
implement the property explicitly, so only consumers of ICollection (who do
not know the specifics of the implementing class) see the property.

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #2
Jay Dee <fi******@gmail.comwrote:
I have bean studying the use of Interfaces, and from what I understand
if you create a class that inherits from an interface, that class must
poses a member that represents each member of the derived interface.

Well this all seams sensible because otherwise it would defy the
principles behind inheritance,

I was studying the class =3FSystem.Collections.Generic.List<T>=3F.
This class states that it derives from the interface
=3FSystem.Collections.Icollection=3F
Therefore I would assume that it would possess all the members that
the interface contains.

This is where I become very confused.
=3FSystem.Collections.Generic.List<T>=3F douse not poses the members:
Yes it does - but it uses explicit interface implementation to define
them. That means that to use those members, you have to use the
reference exactly as an ICollection, rather than as the concrete type.

Search for C# and "explicit interface implementation" for more details.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #3
On May 28, 3:02*pm, Jay Dee <first...@gmail.comwrote:
Hi all.

I have bean studying the use of Interfaces, and from what I understand
if you create a class that inherits from an interface, that class must
poses a member that represents each member of the derived interface.
A class do not inherit from an interface, it does implement it. That
how a class can implement (and behave) like more than one interface
AND inheriting from only one class.
Well this all seams sensible because otherwise it would defy the
principles behind inheritance,
see the above remark

Could anybody please explain this to me?
I think I know the answer, but I would like to see your code first,
Hint, take a look at the difference between explicitely and implicit
interface implementation
Jun 27 '08 #4
Ignacio Machin ( .NET/ C# MVP ) wrote:
On May 28, 3:02 pm, Jay Dee <first...@gmail.comwrote:
>Hi all.

I have bean studying the use of Interfaces, and from what I
understand if you create a class that inherits from an interface,
that class must poses a member that represents each member of the
derived interface.

A class do not inherit from an interface, it does implement it. That
how a class can implement (and behave) like more than one interface
AND inheriting from only one class.
>Well this all seams sensible because otherwise it would defy the
principles behind inheritance,

see the above remark
I believe the OP was referring to the Liskov Substitution Principle. And in
that sense, classes are as much a subtype of any interfaces as of the base
class(es). They don't, however, inherit any behavior from interfaces. Many
languages have only one means of subtyping, which is usually called
"inheritance". .NET distinguishes between behavioral inheritance
(derivation) and interface inheritance (implementation), however.

>
>Could anybody please explain this to me?

I think I know the answer, but I would like to see your code first,
Hint, take a look at the difference between explicitely and implicit
interface implementation

Jun 27 '08 #5

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

Similar topics

21
by: Franco Gustavo | last post by:
Hi, Please help me to understand this, because I don't see what I'm missing. I was reading a lot of examples on Internet that explain that C# doesn't implement multiple inheritance it...
42
by: Franco Gustavo | last post by:
Hi, Please help me to understand this, because I don't see what I'm missing. I was reading a lot of examples on Internet that explain that C# doesn't implement multiple inheritance it...
21
by: Franco Gustavo | last post by:
Hi, Please help me to understand this, because I don't see what I'm missing. I was reading a lot of examples on Internet that explain that C# doesn't implement multiple inheritance it...
21
by: Franco Gustavo | last post by:
Hi, Please help me to understand this, because I don't see what I'm missing. I was reading a lot of examples on Internet that explain that C# doesn't implement multiple inheritance it...
27
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.