0

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?

Harold_Finch
  • 682
  • 2
  • 12
  • 33
  • 7
    This example is too contrived (and not valid code) to give an answer - it always depends on the specific circumstances whether to use inheritance or composition (this is what class `B` is doing), see also: https://stackoverflow.com/questions/2399544/difference-between-inheritance-and-composition – UnholySheep Jun 25 '20 at 09:50
  • Your classes `B` and `C` is invalid. You cannot call methods directly is class like that. Did you mean to put Console.WriteLine` inside another method? – Jakob Busk Sørensen Jun 25 '20 at 09:51
  • @BuildItBusk Correct. That was an error – Harold_Finch Jun 25 '20 at 09:53
  • 1
    You should generally [favour composition over inheritence](https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance). As inheritance is generally less flexible. There is even an argument that states that inheritance is a contrived concept that only works in an academic sense and should [just be abandoned.](https://codeburst.io/inheritance-is-evil-stop-using-it-6c4f1caf5117) – Liam Jun 25 '20 at 09:54
  • I'm prone to agree @Liam – Harold_Finch Jun 25 '20 at 09:56
  • 1
    Both patterns are okay in different situations. Inheritance is an "is" relationship... a *dog* is an *animal*. Composition is a *has* or *can* relationship. A *dog* can *bark* and a dog *has four legs*. Practically speaking, it is usually better to prefer composition over inheritance because a class can be composed of several classes while it can only inherit one. – John Wu Jun 25 '20 at 09:57
  • 2
    You should read design pattern , will you get good exposure how and when use inheritance or object.. – Always_a_learner Jun 25 '20 at 09:58
  • This is the problem with this question I'm afraid, ultimately this is opinion based – Liam Jun 25 '20 at 09:59
  • Yeah this is for my thesis at school. They don't mind us using either approach, as long as your justifications can make sense – Harold_Finch Jun 25 '20 at 10:01
  • @JohnWu Please post that as your answer – Harold_Finch Jun 25 '20 at 10:03
  • Here is an overview of the ["Gang of four" patterns in C#](https://www.dofactory.com/net/design-patterns) – Liam Jun 25 '20 at 10:04

1 Answers1

1

Both patterns are okay in different situations. Inheritance is an "is" relationship... a dog is an animal. Composition is a has or can relationship. A dog can bark and a dog has four legs.

Practically speaking, it is usually better to prefer composition over inheritance because a class can be composed of several classes while it can only inherit one.

John Wu
  • 50,556
  • 8
  • 44
  • 80