How C# Inheritance Saved Me 100 Lines of Code

I am a recent graduate at the beginning of my software development career. I enjoy documenting my learnings through my blogs
I was creating a Blackjack console game in C# as a college assessment. While creating this I created two classes, one for a Player and one for a Dealer. Both the dealer and the player were using the same methods. I encountered a problem when I changed the method code in the Player but not the Dealer, and I questioned why the game would not do what I intended for the dealer. I realised that because the code was duplicated and I only changed it in one place, I would need to change it a second time.
This issue brought me down a search of using a method in two classes but only having to write it once. I found the Stack Overflow question title Best way to share a function between two class files, this talked about something called a Base class and this class could be inherited from other classes. This then brought me down the route of Inheritance, I looked at Inheritance (Derived and Base Class) and Inheritance as resources and implemented this into my Blackjack program.
What is inheritance?
Inheritance is a way of reusing code, saving time and reducing maintenance. Simply put, if creating a new class that will use the same functionality as another class you can have the new class inherit this functionality from what is known as the base class (original class). The new class inheriting from the base class would be known as the derived class. The syntax for inheritance is using the class names for the derived and base class Derived : Base
How this helped me...
Using inheritance helped me to reduce the amount of code that I had to write. I was able to reuse the code defined in one class, from another. In my case, the Player class was the base class and the Dealer inherited its functionality from the Player. This helps with the maintenance of my code, I will no longer run into the issue of having to change the code in multiple places, erasing any duplication. This will be helpful in the future if the code ever needs to be updated or altered.
public class Player {
// Properties
public Card[] Hand { get; set; }
// Methods
public void GiveCards(Card[] cards) { /*removed for simplicity*/ }
public int GetHandValue() { /*removed for simplicity*/ }
private bool HasAnAce() { /*removed for simplicity*/ }
private int NumOfAces() { /*removed for simplicity*/ }
}
public class Dealer : Player {
// Properties
public Deck Deck { get; set; }
// Methods and properties are inherited from the player class
}
The code snippet above shows the properties and methods in the Player class, these are the methods and properties that the Dealer class inherits. It can be seen that Dealer inherits from Player because of the syntax used.
Why the access modifiers used are important when using Inheritance...
For the method to be inherited it should not be set to private, if you want to use it in another class that is not inheriting, this is because the code set with a private access modifier is only accessible in the same class or derived class. To use inheritance the code can be set to public, as public access modifiers are accessible to all classes. For this reason, I chose to use public access modifiers for the methods that would be called in another class other than the derived class and then I used private access modifiers for the methods that were only needed in the base class and derived class.

The image above shows the methods that are available to the Player object in the Program.cs, as you can see the two methods set with a private access modifier cannot be called.
At the end of the day, a Dealer is a type of Player in this game.




