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.
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/
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 ;)
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 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
© 2022 - 2024 — McMap. All rights reserved.
virtual
in the base class. – Citizen"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