Member Access When Inheriting

  • Thread starter Thread starter Noah Coad [MVP .NET/C#]
  • Start date Start date
N

Noah Coad [MVP .NET/C#]

How do you make a member of a class mandatory to override with a _new_
definition? For example, when inheriting from
System.Collections.CollectionBase, you are required to implement certain
methods, such as public void Add(MyClass c).

How can I enforce the same behavior (of requiring to implement a member with
a new return type in an inherited class) in the master class (similar to the
CollectionBase)?

I have a class called "LinearNavigator" which is a generic manager for
Forward and Back operations (like IE) that is inherited to provide
interaction with specific types (like inheriting CollectionBase).
LinearNavigator has methods Next & Back which I want to make mandatory to
implement and a method called Clear that should not be
overridden/re-implemented.

Thanks!
 
Hi,
You need to define LinearNavigator as an abstract class.
You can contain methods and properties as normal, but the
methods for which you wish to enforce implementation,
define them as abstract. Naturally these cannot contain
any implementation in the base class:

The inheriting class must use the override keyword. If
this is omitted, or the implementation is omitted the
compiler reports an error.

For example:

/// Base Class
public abstract class CMostlyAbstract
{
public int x = 1;
public CMostlyAbstract()
{
// TODO: Add constructor logic
}
public abstract void MustOverride();

public void Clear()
{
x=0;
}
}
/// Overriding class
public class COverride : CMostlyAbstract
{
public COverride()
{
// TODO: Add constructor logic
}

public void Reset()
{
base.Clear();
}
public override void MustOverride()
{
// Specific Implementation here!
}
}
/// Usage
COverride co = new COverride();
co.MustOverride();
co.Clear();// Call base method
co.Reset();// call COverride, which calls base


HTH,

Martin
 
So,where would it best to use an Interface over an abstract class and vica
versa?
 
Hi,
It really depends on what you're trying to achieve,
however an Interface contains no implementation
whatsoever and is commonly used just to define a
contract. For example, if you define an interface in a
separate application, then several applications can
contain classes that implement that foreign interface,
and can pass objects to one another that may be cast to
that interface and the correct methods called against
them.
An interface could not have solved all of Noah's
requirements, because he had a Clear() method that must
be implemented in the base class. However he could have
defined an interface, and inherited that in his base
class. This scenario would have been useful for remoting
or distributed situations, but would not have mattered
much as the key element was providing a base class with
some overridable and some sealed methods rather than
defining an abstract contract.

HTH,

Martin
 
This queston was posted just a few days ago, though I'm not sure there
is a definitive answer.

I use iterfaces when I need to perform the same method on disparate,
hierarchically unrelated classes. As an example, I may need to call a
method called Validate on different types of business objects. The
business objects are not related at all, but each need to be validated
before taking some other action. By imherinting an interface called
IValidate, I can call the Validate method on each of these objects
without knowing their real type, by casting each of them to an
IValidate type and calling the method. If the object doesn't implent
the IValidate interface, then I skip the call. These could be objects
contained in an arraylist or other collection type.

I use an abstract class if I want to force an object to implement an
overridden method, but when there are other characteristics that a
class would share with its base class.

Jonathan Schafer
 
Okay, this is not what I'm looking for. Maybe an abstract class, but the
members can't be abstract since I do need an application of the method in
the base class.

Just like CollectionBase does implement IList's Add & Remove methods, which
you have to override in an inherited class. But in CollectionBase, you have
to actually cast to IList to get access to the Add & Remove methods.

-Noah
 
I don't think I'm understanding something...
Do you just mean explicit interface implementation? That's what
CollectionBase uses for IList (as you mentioned, you have to cast
CollectionBase to an IList to use it).

But in what way do you have to override ILists's Add & Remove in
CollectionBase. This compiles just fine (doesn't do much, granted).

class Class1 : System.Collections.CollectionBase
{
public Class1()
{
}
public static void Main()
{
Class1 class1 = new Class1();
}
}

--mike
 
Okay, well when defining your own implementation of Add & Remove, the 'new'
or 'override' keyword is not nessasary. How is this accomplished?

-Noah
 
Maybe the Stragegy Design Pattern is what you really need?

This allows you to provide an algorithm in an abstract base class which
calls abstract methods.

Derivatives supply the implementation of the methods that get called in the
base class' algorithm.

For example (bastardized):

using System;
namespace TestApp

{

class MainClass

{

[STAThread]

static void Main(string[] args)

{

ParentClass p = new ADerivative();

p.DoSomething();

Console.Read();

}

}



public abstract class ParentClass

{


public void DoSomething()

{

for (int i=0;i<100;i++)

{

SuppliedInDerived(i);

}

}

public abstract void SuppliedInDerived(int i);

}



public class ADerivative : ParentClass

{

public override void SuppliedInDerived(int i)

{

Console.WriteLine("this example sucks: " + i);

}

}

}
 
CollectionBase is an abstract class. It cannot even be instantiated.
CollectionBase also happens to implements IList.

You can recreate this behavior in your object model too.

You can also have a derived class call its base class's methods, as well as
override them (or neither). You can even shadow the base classes in derived
classes, also this is not recommended.

I think all the flexibility you need is there. But perhaps I am missing
something in the original question. Maybe you wouldn't mind restating it
with a code sketch example to make it crystal clear what you want?
 
Thanks for all your help and information. I haven't found quite what I'm
looking for yet and this thread is getting old, so I'll reconsider my
question, try to study and learn some more, and repost it more propperly
stated at a later time. Thanks once again!

-Noah
 
Back
Top