Coding: Understanding Inheritance
When I started learning programming in college I had no idea what I was doing, and to be honest, the teaching style was not very effective. Most of my classmates were in the same boat, we had to google a lot and work with each other to try to understand the principles of programming. After many years of coding and learning I was able to grasp the concepts. I am going to attempt to explain these principles in the simplest terms.
As mentioned in this other post [Programming Languages: Differences] one of the core principles of Object-Oriented programming is the concept of inheritance (A feature that represents the “is a” relationship between different classes. It enables new objects to take on the properties of existing objects).
In my opinion, this concept is easy to understand in the real world but it was one of the hardest to understand in code.
The easiest way to explain it in simple words is if I tell you that a Ford Mustang is a vehicle so is a Chevy Silverado. They have different attributes that describe them such as engine size, transmission type, drivetrain, but both of them are vehicles (Check this example https://www.w3schools.com/cs/cs_inheritance.asp).
This is an important concept in programming since it reduces duplication and allows our system to be dynamic.
You can reuse the inherited methods, extend them, or modify the behavior of the base class.
As you can see in this example, the "CoffeeMachine" is the base class, it has "BrewCoffee" method and some other common classes to display the user when his/her coffee is being brewed and when the coffee is done brewing.
public class CoffeeMachine
{
public Coffee Coffee;
public string CupSizeName;
public CoffeeMachine(Coffee coffee, CupSizeEnum cupSizeEnum)
{
Coffee = coffee;
switch (cupSizeEnum)
{
case CupSizeEnum.Small:
CupSizeName = "Small";
break;
case CupSizeEnum.Medium:
CupSizeName = "Medium";
break;
case CupSizeEnum.Large:
CupSizeName = "Large";
break;
}
}
public Coffee BrewCoffee()
{
Coffee brewedCoffee = Coffee;
DisplayProcessStep();
return brewedCoffee;
}
public void DisplayProcessStep()
{
Console.WriteLine($"Brewing {CupSizeName} cup of {Coffee.Name}");
}
public void DisplayFinalStep()
{
Console.WriteLine("Enjoy your fresh cup of joe!");
}
}
These attributes and methods can be inherited by other classes such as the "SpecialtyCoffeeMachine" to brew other drinks such as Espresso, Pour Overs, etc.
As you can see the "SpecialtyCoffeMachine" (Derived) inherits the "CoffeeMachine" (Base) class which includes its methods (classes that inherit from base classes are called derived classes). From here we can modify them or simply use the base inheritance.
public class SpecialtyCoffeeMachine : CoffeeMachine
{
public SpecialtyCoffeeMachine(Coffee coffee, CupSizeEnum cupSizeEnum) : base(coffee, cupSizeEnum)
{
}
public Coffee BrewCoffee()
{
Coffee brewedCoffee = Coffee;
switch (Coffee.CoffeeSelection)
{
case CoffeeSelectionEnum.EspressoShot:
brewedCoffee = BrewEspressoShot();
break;
case CoffeeSelectionEnum.Americano:
brewedCoffee = BrewAmericano();
break;
case CoffeeSelectionEnum.Drip:
brewedCoffee = BrewDrip();
break;
case CoffeeSelectionEnum.PourOver:
brewedCoffee = BrewPourOver();
break;
default:
Console.WriteLine("Coffee Selection not chosen.");
break;
}
int count = 0;
while (count <= 4)
{
Thread.Sleep(1000);
Console.Write(".");
count++;
}
brewedCoffee.IsBrewed = true;
return brewedCoffee;
}
private Coffee BrewEspressoShot()
{
DisplayProcessStep();
//Do whatever needs to be done to brew espressoShot
return Coffee;
}
private Coffee BrewAmericano()
{
DisplayProcessStep();
//Do whatever needs to be done to brew americano
return Coffee;
}
private Coffee BrewDrip()
{
DisplayProcessStep();
//Do whatever needs to be done to brew drip coffee
return Coffee;
}
private Coffee BrewPourOver()
{
DisplayProcessStep();
//Do whatever needs to be done to brew pour over coffee
return Coffee;
}
}
You can see how we are modifying the "BrewCofee" method to be able to brew specialty coffees but we are reusing the "DisplayProcessStep" method from the base class.
When we instantiate the "SpecialtyCoffeeMachine" (Derived) class and we can access methods in this class or the "CoffeeMachine" (Base) class.
var coffeeMachine = new CoffeeMachine(selectedCoffee, cupSizeEnum);
var brewedCoffee = coffeeMachine.BrewCoffee();
coffeeMachine.DisplayFinalStep();
I hope that this small code snippet helps understand the principle of inheritance a little bit better.
You check out the whole code in this repository.