When child classes inherit from a parent class, is there a way to use different data types for an inherited field to add more functionality to the field in the different child classes? And then use those child classes as one single data type that can be used as parameters in functions?
I need to create two objects that have some similarities but are different enough to warrant having different classes. So I think having a base class for them would be suitable. I have two props created in the Base class 'ParentBase' which contain shared things for the child classes to use, and the child classes need to add further functionality to these shared props.
E.g. The Settings field from the ParentBase should be extended in the Parent1 and Parent2 classes for their own unique needs. I feel like I need to create new data types for extending the Settings field for both Parent1 and Parent2 classes.
class ParentBase
{
public ChildA Settings { get; set; }
public ChildX MoreSettings { get; set; }
// lots of shared props here that won't be extended in inheriting classes
public void SomeFunction()
{
// The inheriting class's Settings and MoreSettings props should be available to access here
// even though their data types are different to the base class's Settings and MoreSettings
// data types
}
}
class Parent1 : ParentBase
{
public ChildB Settings { get; set; }
public ChildY MoreSettings { get; set; }
}
class Parent2 : ParentBase
{
public ChildC Settings { get; set; }
public ChildZ MoreSettings { get; set; }
}
class ChildA { // base props and methods in here }
class ChildB : ChildA { // Parent1 specific functionality }
class ChildC : ChildA { // Parent2 specific functionality }
class ChildX { // base props and methods in here }
class ChildY : ChildX { // Parent1 specific functionality }
class ChildZ : ChildX { // Parent2 specific functionality }
I also need to create functions outside the base class that will take in either Parent1 or Parent2 objects as parameters. E.g:
public void Calculate(SomeSharedType Parent1/Parent2 instance)
{
// need to access the Settings and MoreSettings properties here, and the base class's Setting should suffice,
// although it would be nice to access the inheriting class's Settings and MoreSettings properties
}
Is there a way for me to achieve this with inheritance or interfaces?
Does this answer your question?
class ParentBase<T,U>
{
public virtual T Settings { get; set; }
public virtual U MoreSettings { get; set; }
}
class Parent1 : ParentBase<ChildB, ChildY>
{
public override ChildB Settings { get; set; }
public override ChildY MoreSettings { get; set; }
}
class Parent2 : ParentBase<ChildC, ChildZ>
{
public override ChildC Settings { get; set; }
public override ChildZ MoreSettings { get; set; }
}
Although you should note that the override is necessary only when you want to change the properties behaviour but in order to only change the types the following code would be enough:
class ParentBase<T,U>
{
public T Settings { get; set; }
public U MoreSettings { get; set; }
}
class Parent1 : ParentBase<ChildB, ChildY>
{
}
class Parent2 : ParentBase<ChildC, ChildZ>
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With