1

Let's say you have interface IFoo and some member Member. The class which implements it is capable of implementing the member coming from interface and at the same time adding "new" member with exactly the same name.

It is really great. Now I would like to pull something similar but not coming from interface but from abstract class. I would expect similar behavior as with interface -- having instance of abstract class, the original Member would be seen, having instance of derived class the "new" member would be seen and original would be hidden.

I have something like this in mind:

abstract class AFoo
(
  public abstract string Member { get; }
}

class Foo : AFoo
{
  public override string Member { get; } // this is the one coming from AFoo

  public new int Member { get; } // does not work (try 1)
  string AFoo.Member { get; } // works only with interfaces (try 2)
}

Is there any mechanism to allow this?

Update: there are 2 members in Foo, I just wrote two tries. The usage would be same as with interface:

var foo = new Foo();
AFoo afoo = foo;
foo.Member; // gets int
afoo.Member; // gets string
astrowalker
  • 3,123
  • 3
  • 21
  • 40
  • 6
    Why do you want to do this? What would `myFoo.Member` even mean, if you have two properties with the same name in the same class? – Thomas Mar 09 '20 at 13:37
  • I guess the OP doesn't understand the difference between overriding and shadowing a method? You can't do both at the same time. –  Mar 09 '20 at 13:38
  • 1
    Create an intermediate class (that inherits from AFoo) that implements `AFoo.Member`. Make `Foo` inherit from the intermediate class and implement the new `Member`. – Flydog57 Mar 09 '20 at 13:40
  • Use cases for the `new modifier` are few and far between.,, What is the motivation for this requirement? – Matthew Watson Mar 09 '20 at 13:46
  • 1
    You can't have two different members of the same type with the same signature. Explicit interface implementation is the exception of the rule, designed to enable implementing different interfaces with matching members signatures. – Zohar Peled Mar 09 '20 at 13:47
  • 2
    I'd really like to see you modify your question to include the desired usage. Show how you want to access this object. Show where and how you want to be able to access each of the three `Member` properties you have defined. – Wyck Mar 09 '20 at 13:50
  • @Wyck., ah, sorry, my bad, those were just 2 tries, so total number of members are two, not three. And I updated the question to show the usage, but it is basically the same as with EII. – astrowalker Mar 09 '20 at 13:54
  • You've missed two cases. A method of `Foo` refers to `this.Member`, which one will it get? Also, a method of `AFoo` refers to `this.Member`, which one will it get? – Wyck Mar 09 '20 at 14:12
  • Why would you want to do such a thing? – Zohar Peled Mar 09 '20 at 14:23
  • @ZoharPeled: Consider that the abstract class is `AbtractArtist` and the abstract member is `Draw`. Now consider that you want to implement an `OldWestCowboyArtist`. You what to have the artist implementation of `Draw` as well as the cowboy movie implementation of `Draw` (the command that indicates that it's time to pull one's gun out of one's holster) – Flydog57 Mar 09 '20 at 14:40
  • @Flydog57 I don't consider absolute theoretical (not to say down right absurd) case as valid use cases. Sorry. – Zohar Peled Mar 09 '20 at 15:30
  • @ZoharPeled: My point was to make it a little humorous, not absurd. But, overall, the reasoning is sound. The same term (like `Draw`) may have multiple semantic meanings in multiple contexts - and you may have a class that spans those contexts. I've been in this business about 35 years and had to do something like this twice. It was very simple in C#, it was a royal PITA back when I was a C++ programmer in the 1990s. – Flydog57 Mar 09 '20 at 15:34
  • @Flydog57 " I've been in this business about 35 years and had to do something like this twice. " my point exactly. Though my experience is only 20 years I've yet to encounter such a need - so I guess if the need arose twice in our combined 55 years of experience - it's quite safe to assume it's a rare situation. The nice thing in programming, though, is that you're not chained to English - you could just call one method x and the other y (of course, meaningful names, but you get the point...) – Zohar Peled Mar 09 '20 at 15:48
  • @Wyck, the resolution should be **exactly** as with EII. – astrowalker Mar 10 '20 at 06:10

1 Answers1

2

Implement an intermediate class (InterFoo) that inherits the abstract class and that implements the required member:

class InterFoo : AFoo
{
    public override string Member { get; } 
}

Then have your class inherit from that intermediate class and have it shadow the base class version:

class Foo : InterFoo
{
    public new int Member { get; }
}

That Should work.

Flydog57
  • 6,851
  • 2
  • 17
  • 18