Reputation: 374
I have a base class and many child class's. In every child class I use Object A - I have therefore put it in the baseclass. In half the child class's I use Object B I have therefore not put B in the baseclass. I want my baseclass to include as much of the functionally as possible and I therefore want the methods in the baseclass to be able to 'do something' If the Object B is defined. Does that make sense?
I might be looking at the problem from the wrong angle and I hope you can guide me in the right direction. Let me know if I didnt explain the problem well enough
I have tried to put Object B in the baseclass but I dont want every childclass to be able to use Object B.
abstract class BaseClass
{
public Object A { get; set; }
protected virtual void SomeMethod()
{
if (!(A is null))
{
//Do thing with A
//This works fine
}
if (!(B is null))
{
//Do thing with B
//This doesnt work - But I want to it work
}
}
}
class ChildClass : BaseClass
{
public Object B { get; set; }
}
Upvotes: 3
Views: 70
Reputation: 1899
I would like to add few points about class, abstract class & interfaces:
Re-usability: When you talk about base class and want to add as much as functionality into it so that the child classes can reuse it, please ensure that you are not violating the Single Responsibility Principle. Instead of stuffing your class with everything, consider it's role and add the responsibility accordingly.
Inheritance vs Direct Usage: You talked about using ObjectB into the base class but also want to restrict the usage to limited child classes and not available to all. This is not possible and also not a good approach. Instead, create a separate class for ObjectB and use it inside the specific child classes using Dependency Injection. This way, you are able to use the instance of Test in child classes and also not creating a tight coupling:
//Class to be used in specific child classes
public class Test
{
public object B { get; set; }
}
//Child class implementation using Constructor Injection
public class SpecificChild
{
public Test _test { get; set; }
public SpecificChild(Test test)
{
_test = test;
}
}
I can also use inheritance in the above code, but it is not good as C# doesn't support multi-level inheritance and you may need to inherit some other class in future
Virtual/Override conflict: When you are overriding a virtual method in child class and completely rewriting it, it's better to use an abstract method and define only the method definition so that the implementing class can write it accordingly.
abstract class BaseClass
{
public Object A { get; set; }
//protected virtual void SomeMethod()
//{
// if (!(A is null))
// {
// //Do thing with A
// //This works fine
// }
// if (!(B is null))
// {
// //Do thing with B
// //This doesnt work - But I want to it work
// }
//}
public abstract void SomeMethod();
}
class ChildClass : BaseClass
{
public Object B { get; set; }
public override void SomeMethod()
{
// if (!(A is null))
// {
// //Do thing with A
// //This works fine
// }
// if (!(B is null))
// {
// //Do thing with B
// //This doesnt work - But I want to it work
// }
}
}
Extra Reading: If you are confused about Abstract class and Interface, I've written a post long time ago that can be useful to you: https://www.c-sharpcorner.com/UploadFile/d0e913/abstract-class-interface-two-villains-of-every-interview/
Upvotes: 1
Reputation: 82534
The simplest solution would be to have another class deriving from your base class adding the B
property and related functionality to it, and have all the classes that share this property and functionality derived from that class instead of directly from the base class.
Something like this:
abstract class BaseClassB : BaseClass
{
public object B {get;set;}
protected override void SomeMethod()
{
base.SomeMethod();
if (B != null)
{
//Do thing with B
//
}
}
}
Upvotes: 5