10

Is there a way that a derived class could inherit only a few of all the base class members..in C#? If such maneuver is possible, please provide some example code.

CSharp4eto
  • 147
  • 2
  • 6
  • 1
    No. Inheritance is inheritance. A derived class *is* an instance of a base class, and therefore contains everything the base class contains. What are you actually trying to accomplish here? – David May 10 '16 at 17:30
  • 1
    A class inherits all base members. If you want to pick and choose which members are overridable, you can mark them as `virtual` in the base class. – Jonesopolis May 10 '16 at 17:31
  • I would suggest you create 2 different classes and mark the one that you don't want to be inherited seal :) – Auguste May 10 '16 at 17:34
  • This may help: http://stackoverflow.com/questions/614818/what-is-the-difference-between-public-private-protected-and-nothing – Andrew Truckle May 10 '16 at 17:34
  • No, it is not possible. There are a myriad of solutions to get around this, but it is impossible to offer any suggestions with so little information. – Kevin Burdett May 10 '16 at 17:36
  • Yes, that's what I thought...overriding a virtual, but I was just wondering if there is a hack way or something, but if there is such a way ..i guess usual devs are less likely to know it. Or maybe microsoft should work on creating a way to do so in means of shortcut. – CSharp4eto May 10 '16 at 17:36
  • 2
    This is just an example of the [X Y Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Jonesopolis May 10 '16 at 17:37
  • Well @KevinBurdett, then you could share at least one. – CSharp4eto May 10 '16 at 17:39
  • @CSharp4eto you could split the base class into multiple classes, possibly inheriting from one another, such that the true base class contains only that which should be inherited by all descendents. You could also transition this to an interface based object composition model... I don't intend to come across as a jerk, but having a bunch of people on the internet randomly guess at possible solutions is unlikely to get you what you need. If you provide more details about your situation (ideally with code) then the community can offer informed suggestions. – Kevin Burdett May 10 '16 at 17:44
  • @CSharp4eto: `"I was just wondering if there is a hack way or something"` - I strongly advise against seeking such things in your code. Clever hacks are fun to tinker with, but near impossible to meaningfully support in the real world. `"Or maybe microsoft should work on creating a way to do so in means of shortcut"` - Unlikely. This sounds like an invented problem which already has a solution in a better design. Microsoft isn't going to change C# to allow bad design practices, but you can change your design to follow good practices. – David May 10 '16 at 18:33

3 Answers3

17

Is there a way that a derived class could inherit only a few of all the base class members..in C#?

Yes. Make a base class that has one method, one constructor and one destructor. It has three new members, plus the heritable members of its base class. Now derive a class from that. The constructor and destructor will not be inherited; all the other members will. Therefore it is possible to create a derived class which inherits only some of its base class's members.

I suspect that answer is unsatisfying.

If your question is actually "is there a way that a base class can restrict what heritable members are inherited by a derived class?" the answer is no. Derived classes inherit all heritable members of base classes, regardless of their accessibility.

If your question is "is there a way that a derived class can choose which heritable members to inherit from a base class?" the answer is no. Derived classes inherit all heritable members of base classes, regardless of their accessibility.

Further reading, if this topic interests you:

https://ericlippert.com/2011/09/19/inheritance-and-representation/

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • 1
    I love your answer because there is a sadistical part of myself that pictures a special part of hell where bad programmers go, and they are forced to maintain that kind of code forever. – Geeky Guy May 10 '16 at 17:48
2

When you make a type inherit from another, you get everything - both the good and the "bad" bits from the parent type ("bad", in this context, meaning something you didn't want to have).

You can hide something from the parent class in the child class through the new modifier. However, take this advice from years of experience... More often than not this leads to a lot of work being spent on doing workarounds in the way the child class works. You'll spare yourself from a lot of trouble if instead of going this way, you redesign your classes.

If a child type has to clip off functionalities from a parent type, you probably have a design flaw in the parent. Reshape it to have less funcionality. You can have its different features redistributed among different children. A class doesn't always have to be an only child, you know ;)

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
  • 2
    I upvoted just for the warning on the "new" modifier. Nothing but pain comes from that. – Wesley Long May 10 '16 at 17:44
  • Meh, you can hide inherited members reusing the identifiers to implement a private member... but this isn't a very good solution after all.. – Matías Fidemraizer May 10 '16 at 17:46
  • 1
    There are valid uses for "new". Two examples: (1) emulating return type covariance, (2) indicating to the compiler that a potential brittle base class failure has been analyzed and found to be actually correct. But yes, I agree with you that in general, if you're designing a type hierarchy in which you think that "new" is the right thing to do, likely there is a more elegant way to do it. The advice to favour composition over inheritance is good in these cases. – Eric Lippert May 10 '16 at 17:53
  • @EricLippert agreed. I think that it takes skill and experience to make that kind of decision. So my reasoning goes like, "if you have to ask about this, you are not ready to do it yet." – Geeky Guy May 10 '16 at 17:55
  • @EricLippert The worst part is a lot of devs ignore that `new` in that case is just *identifier re-using* and they don't understand the cons. For example, the effect of upcasting an instance of some derived type to its base where the derived type was re-using an identifier............. – Matías Fidemraizer May 10 '16 at 18:57
  • Hmm, I'm using the new identifier to override a base viemmodels virtual properties to become real properties in a child viewmodel which causes them to be loaded by entity framework when I use said child viewmodel. If I could skim out some of the original parent fields then that would cut down database access – Worthy7 Aug 26 '16 at 01:58
0

No, it's not possible. Do you imagine a Cat deriving Animal and the child (the Cat) deciding what's interesting from animals or not? A cat is an animal and this can't be changed.

BTW, interfaces can be used to hide details. For example:

public interface ISome
{
    string Text { get; set; }
}

public class A : ISome
{
    public string Text { get; set; }
    public string Text2 { get; set; }
}

public class B : A
{
}

// This is an upcast. You're reducing the typing of an instance of B
ISome a = new B();
string text2 = a.Text2; // Error, Text2 isn't a property of ISome
string text = a.Text; // OK, Text is a property of ISome
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • I'm trying to think of how I could use this to avoid having to apply all my attributes over and over again in an ASPMVC app when using lots of viewmodels. – Worthy7 Aug 26 '16 at 02:01
  • @Worthy7 Can't you apply these attributes in an abstract class which implements some interfaces? Thus, view models could inherit that base class and you'll avoid messing with attributes across all classes.. – Matías Fidemraizer Aug 26 '16 at 09:11