473,788 Members | 2,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is abstract method implemented or not?

There's something I don't understand about how the CollectionBase class is
defined. I can declare:

class Derived : CollectionBase
{
}

I can instantiate this Derived class:

Derived derived = new Derived();

I cannot call an Add method as I haven't declared it:

derived.Add(1); //compiler error: Derived does not contain a definition
for Add

However CollectionBase derives from IList, IList declares Add as public
abstract, and I can successfully call Add through the IList interface:

IList ilist = derived;
ilist.Add(1);

So what is happening here:

* If neither CollectionBase nor Derived define Add, then why can I
instantiate Derived, and call Add through the IList interface?

* Or if CollectionBase does implement Add, then why can't I see it in the
Object browser and why do I get an error when I try to call it?

I understand how I'm *supposed* to use CollectionBase (I'm supposed to
define an Add method in a derived class). What I don't understand is how
CollectionBase itself is is declared or implemented: how is it that a
Derived : CollectionBase can derive from IList, and be instantiable, when
the Add method isn't defined or implemented in Derived nor in
CollectionBase, but is declared as abstract in IList?
Nov 16 '05 #1
7 2786
Well Add isn't really instantiable via Derived (because of Abstract) and
hence doesn't appear
to be available however you cheated when trying to access it via the
Interface. As far as I know
interfaces simply tell you whats available - if the programmer didn't
actually impliment the method correcly
the interface won't know. What happens if you run this code, does it crash
when calling Add? It will probably
just do nothing as Add is in the base class and does nothing - it might be
there but its probably empty.

C
http://www.typemismatch.com/
(For Developers!)

"Christophe r Wells" <ch************ ***@invalid.inv alid> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
There's something I don't understand about how the CollectionBase class is
defined. I can declare:

class Derived : CollectionBase
{
}

I can instantiate this Derived class:

Derived derived = new Derived();

I cannot call an Add method as I haven't declared it:

derived.Add(1); //compiler error: Derived does not contain a definition for Add

However CollectionBase derives from IList, IList declares Add as public
abstract, and I can successfully call Add through the IList interface:

IList ilist = derived;
ilist.Add(1);

So what is happening here:

* If neither CollectionBase nor Derived define Add, then why can I
instantiate Derived, and call Add through the IList interface?

* Or if CollectionBase does implement Add, then why can't I see it in the Object browser and why do I get an error when I try to call it?

I understand how I'm *supposed* to use CollectionBase (I'm supposed to
define an Add method in a derived class). What I don't understand is how
CollectionBase itself is is declared or implemented: how is it that a
Derived : CollectionBase can derive from IList, and be instantiable, when
the Add method isn't defined or implemented in Derived nor in
CollectionBase, but is declared as abstract in IList?

Nov 16 '05 #2
Christopher Wells <ch************ ***@invalid.inv alid> wrote:
There's something I don't understand about how the CollectionBase class is
defined.


It's called explicit interface implementation - it's quite bizarre, to
be honest.

See http://www.jaggersoft.com/csharp_standard/20.4.1.htm for more
information.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
ilPostino <ne**@ip80.co m> wrote:
Well Add isn't really instantiable via Derived (because of Abstract) and
hence doesn't appear
to be available however you cheated when trying to access it via the
Interface. As far as I know
interfaces simply tell you whats available - if the programmer didn't
actually impliment the method correcly
the interface won't know. What happens if you run this code, does it crash
when calling Add? It will probably
just do nothing as Add is in the base class and does nothing - it might be
there but its probably empty.


No, it's there all right - it validates the value and then adds it to
the list.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

when you explicitly implement an interface method, that method is not accessible through the class itself, only accessible through casting to the interface

----- ilPostino wrote: ----

Well Add isn't really instantiable via Derived (because of Abstract) an
hence doesn't appea
to be available however you cheated when trying to access it via th
Interface. As far as I kno
interfaces simply tell you whats available - if the programmer didn'
actually impliment the method correcl
the interface won't know. What happens if you run this code, does it cras
when calling Add? It will probabl
just do nothing as Add is in the base class and does nothing - it might b
there but its probably empty
http://www.typemismatch.com
(For Developers!

"Christophe r Wells" <ch************ ***@invalid.inv alid> wrote in messag
news:%2******** ********@TK2MSF TNGP09.phx.gbl. .
There's something I don't understand about how the CollectionBase class i
defined. I can declare
class Derived : CollectionBas
I can instantiate this Derived class
Derived derived = new Derived()
I cannot call an Add method as I haven't declared it
derived.Add(1); //compiler error: Derived does not contain

definitio for Ad
However CollectionBase derives from IList, IList declares Add as publi abstract, and I can successfully call Add through the IList interface
IList ilist = derived

ilist.Add(1)
So what is happening here
* If neither CollectionBase nor Derived define Add, then why can

instantiate Derived, and call Add through the IList interface
* Or if CollectionBase does implement Add, then why can't I see it i

th Object browser and why do I get an error when I try to call it
I understand how I'm *supposed* to use CollectionBase (I'm supposed t

define an Add method in a derived class). What I don't understand is ho
CollectionBase itself is is declared or implemented: how is it that
Derived : CollectionBase can derive from IList, and be instantiable, whe
the Add method isn't defined or implemented in Derived nor i
CollectionBase, but is declared as abstract in IList

Nov 16 '05 #5
What you see is whats called an explicit interface. The idea is to hide
the various interfaces from the developer unless you *explicitly* decide
to use the interface method. Its a language design choice to help
*users* of a particular class focus only on interfaces that would be
useful to them (from the perspective of the *provider* ofcourse)

Christopher Wells wrote:
There's something I don't understand about how the CollectionBase class is
defined. I can declare:

class Derived : CollectionBase
{
}

I can instantiate this Derived class:

Derived derived = new Derived();

I cannot call an Add method as I haven't declared it:

derived.Add(1); //compiler error: Derived does not contain a definition
for Add

However CollectionBase derives from IList, IList declares Add as public
abstract, and I can successfully call Add through the IList interface:

IList ilist = derived;
ilist.Add(1);

So what is happening here:

* If neither CollectionBase nor Derived define Add, then why can I
instantiate Derived, and call Add through the IList interface?

* Or if CollectionBase does implement Add, then why can't I see it in the
Object browser and why do I get an error when I try to call it?

I understand how I'm *supposed* to use CollectionBase (I'm supposed to
define an Add method in a derived class). What I don't understand is how
CollectionBase itself is is declared or implemented: how is it that a
Derived : CollectionBase can derive from IList, and be instantiable, when
the Add method isn't defined or implemented in Derived nor in
CollectionBase, but is declared as abstract in IList?


--
Regards,
Dilip Krishnan
MCAD, MCSD.net
dilipdotnet at apdiya dot com
Nov 16 '05 #6
Thanks.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Christopher Wells <ch************ ***@invalid.inv alid> wrote:
There's something I don't understand about how the CollectionBase class is defined.


It's called explicit interface implementation - it's quite bizarre, to
be honest.

See http://www.jaggersoft.com/csharp_standard/20.4.1.htm for more
information.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7
> As far as I know interfaces simply tell you whats available - if the
programmer didn't actually impliment the method correcly the interface won't
know.

What puzzled me is that, if the derived class didn't implement the abstract
method at all, then the method would be what C++ calls "pure abstract", and
I shouldn't have been allowed to even instantiate the derived class.
What happens if you run this code, does it crash when calling Add?
No: it goes ahead and successfully adds something. Apparently, if you want
to prevent IList.Add from being used to Add an object of the wrong type to
your class derived from CollectionBase, then you can override OnInsert and
similar methods.
"ilPostino" <ne**@ip80.co m> wrote in message
news:uE******** ******@TK2MSFTN GP12.phx.gbl... Well Add isn't really instantiable via Derived (because of Abstract) and
hence doesn't appear
to be available however you cheated when trying to access it via the
Interface. As far as I know
interfaces simply tell you whats available - if the programmer didn't
actually impliment the method correcly
the interface won't know. What happens if you run this code, does it crash
when calling Add? It will probably
just do nothing as Add is in the base class and does nothing - it might be
there but its probably empty.

C
http://www.typemismatch.com/
(For Developers!)

"Christophe r Wells" <ch************ ***@invalid.inv alid> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
There's something I don't understand about how the CollectionBase class is defined. I can declare:

class Derived : CollectionBase
{
}

I can instantiate this Derived class:

Derived derived = new Derived();

I cannot call an Add method as I haven't declared it:

derived.Add(1); //compiler error: Derived does not contain a

definition
for Add

However CollectionBase derives from IList, IList declares Add as public
abstract, and I can successfully call Add through the IList interface:

IList ilist = derived;
ilist.Add(1);

So what is happening here:

* If neither CollectionBase nor Derived define Add, then why can I
instantiate Derived, and call Add through the IList interface?

* Or if CollectionBase does implement Add, then why can't I see it in

the
Object browser and why do I get an error when I try to call it?

I understand how I'm *supposed* to use CollectionBase (I'm supposed to
define an Add method in a derived class). What I don't understand is how
CollectionBase itself is is declared or implemented: how is it that a
Derived : CollectionBase can derive from IList, and be instantiable, when the Add method isn't defined or implemented in Derived nor in
CollectionBase, but is declared as abstract in IList?


Nov 16 '05 #8

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

Similar topics

17
6646
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products. So I shall have an Abstract Base called 'Car' implemented by Toyota, Ford, and Buick. Further I'd like to enable to client to say Car *factory;
8
2265
by: Vishal Gandhi | last post by:
Hi , Please help me by advising an real life scenario where Abstract Classes should be used over Interfaces or vice versa . Whats the basic difference between Abstract Class and interface other then instantiation ? Many Thanks Vishal Gandhi
10
2569
by: Bjorn | last post by:
I'm using interfaces in C++ by declaring classes with only pure virtual methods. If then someone wants to implement the interface they needs to inherit from the class. If the implementing class forgets to implement some method I normally get a compile error(if the class is created in some way of course). When using the Visual Studio 6 compiler however, it does not generate a compile error in this case: - I have a implementing class A...
7
228
by: Christopher Wells | last post by:
There's something I don't understand about how the CollectionBase class is defined. I can declare: class Derived : CollectionBase { } I can instantiate this Derived class: Derived derived = new Derived();
6
5804
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove the need to have abstract class types in C#? Derived classes from abstract base classes must overrided the abstract method defininition anyway, so why not just provide a complete definition for the abstract method when defining the containing...
4
1512
by: Rachel Devons | last post by:
All, I'm struggling with an OOP concept. Let me try to define what I'm wanting by using some classic examples. Let's say that I have classes called Square & Circle that derive from class Shape. Within class Shape, it has a method called Save to save the object to disk. It's defined in Shape because the logic to save the shapes is uniform across all shapes. Furthermore, let's say I've defined an interface called IDraw that includes
0
2681
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is mainly a wrapper class. It helps
5
1769
by: Jose Fernandez | last post by:
Hello I have been reading about the differences between virtual and abstract keywords... What i understand is that with VIRTUAL keyword, you can actually write the whole method and override it if needed... with abstract, you just declare the method name and must write the code in the inherited class. Am I right?? public class House{
0
2837
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g., forgetting to implement a method required by the given interface). This is handy when unit tests or...
0
10366
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9967
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.