Please consider the below:
public class A
{
public string MethodA()
{
return "Hello from Class A";
}
}
public class B
{
public void MethodB()
{
ClassA classAObj = new ClassA();
Console.WriteLine(classAObj.MethodA());
}
}
public class C : A
{
public void MethodC()
{
Console.WriteLine(MethodA());
}
}
My question here is, under what circumstance do I create an instance of Class A (as shown in Class B
) or inherit the members of the parent class (Class A
) as shown in Class C
.
I know OOP concepts say use inheritence whenever you want to share the behavior of the members of a parent class in the child class. In this case I'm not changing nor overriding the base class' default definition.
Should I create an object reference to class A or simply inherit Class A?