is abstract method implemented or not?

  • Thread starter Christopher Wells
  • Start date
C

Christopher Wells

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?
 
I

ilPostino

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!)
 
J

Jon Skeet [C# MVP]

ilPostino said:
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.
 
G

Guest

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!
 
D

Dilip Krishnan

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)
 
C

Christopher Wells

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top