Is it possible a class to inherit only some(not all) base class members?
Asked Answered
S

3

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.

Selenodont answered 10/5, 2016 at 17:28 Comment(10)
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?Misgovern
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.Citizen
I would suggest you create 2 different classes and mark the one that you don't want to be inherited seal :)Feune
This may help: #615318Tinatinamou
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.Idyllic
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.Selenodont
This is just an example of the X Y ProblemCitizen
Well @KevinBurdett, then you could share at least one.Selenodont
@Selenodont 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.Idyllic
@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.Misgovern
W
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/

Wang answered 10/5, 2016 at 17:44 Comment(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.Reminisce
R
3

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

Reminisce answered 10/5, 2016 at 17:38 Comment(6)
I upvoted just for the warning on the "new" modifier. Nothing but pain comes from that.Dedicate
Meh, you can hide inherited members reusing the identifiers to implement a private member... but this isn't a very good solution after all..Progesterone
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.Wang
@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."Reminisce
@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.............Progesterone
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 accessUnknowing
N
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
Ninny answered 10/5, 2016 at 17:41 Comment(2)
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.Unknowing
@Unknowing 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..Progesterone

© 2022 - 2024 — McMap. All rights reserved.