Re: a case for multiple inheritance



Tom Spink wrote:

1) Create a generic ProtectedList<T> class which inherits from List<T>
and overrides the write functions (using the new modifier) to change the
access level from public to protected.

Mhm. FYI The 'new' modifier will define a method as hiding a derived one,

Sorry, I didn't complete the description, the 'new protected' modified functions would delegate to the base implementation. I would assume that the compiler would optimize it so that the double function call would be reduced to a single function call resulting in no performance hit.

however, take a look at this:

public class A
{
public void Foo ()
{
Console.WriteLine("Foo in A");
}
}

public class B : A
{
private new void Foo ()
{
Console.WriteLine("Foo in B");
}
}

public class E
{
public static void Main ()
{
B b = new B();
b.Foo();
}
}

The output of this will be "Foo in A", because the accessible method from A
is called. If you change the scope of 'Foo' in B to public, the output
will be "Foo in B" as is expected. And, even more interestingly, if you
cast b to A and call 'Foo':

((A)b).Foo();

You'll get A's implementation of 'Foo'. So, as you can see, using the new
keyword will not give you the desired effect here.

The automatic fallback to the public base implementation, after specifically overriding it, is very disturbing to me. The cast not much of a surprise, assuming you are allowed to perform that cast, I would expect that behavior.


2) Inherit both specialized List<T> by the high-level class
Can you explain this a bit more? I'm not sure why you need multiple
inheritance here. If you want to expose two read-only lists from your
high-level class, you can expose them as properties that return an array of
your list's type, then call the ToArray method of the associated list.

Thanks for the advice, as you can tell, I'm new to C# and I'm looking for these kinds of tips. The idea of multiple inheritance is to not require this kind of massaging and wrapping for every 'ProtectedList' you want to expose. I want to expose all the useful read-only List interface like Exists, Equals, access methods, including custom sort iterators with Predicate, etc., in as simple a way to program as possible. Aggregating, and exposing wrappers is not, in my mind, an efficient way to do this.

.



Relevant Pages

  • Re: Call root base method from doubly derived class?
    ... B would have to expose a method which would expose the ... base method of A, without doing anything else. ... > public class B: A { ... > Bthingy's 'base.foo()' calls class A's foo. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Wrapper class
    ... situations you should probably have a look at the proxy/decorator patterns ... public class FooProxy: Foo{ ... public override void Bar() { ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Newbie question - Disadvantages of java
    ... >> as I am about to switch to java, can somebody tell me the biggest ... public class Foo { ... public Foo getFoo() { ... I.e. primitive types, references and array references? ...
    (comp.lang.java.programmer)
  • Re: static virtual/override
    ... > I sorely miss the feature that permits ... > public class Bar: Foo ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: XmlSerializer: Inheritance and Read-Only Properties
    ... > public class Bar: Foo ... > public string Message ...
    (microsoft.public.dotnet.xml)