C# language: 11. Inheritance

in #programming7 years ago

CC.png

Object-oriented programming consists of four main elements, encapsulation, inheritance, polymorphism and abstraction, encapsulate is behind us time to get to with inheritance and polymorphism.

1. What is inheritance?

What is inheritance? Let’s assume that we want to make a recipe similar to the one we made earlier, we will add only a few additional ingredients to this new one, so the new recipe will also have a list of ingredients from the previous one, so why prescribe cake code from the first cake? We will also use the third most important protected access modifier.

So let’s create a new class for a new cake as we did in the first lesson about classes and call it NewCake. If we want the NewCake class to inherit from the Cake class, we need to write something like this next to the name of the NewCake class:

class NewCake : Cake

When we do that, we will have a bug related to the Cake class constructor, but we will worry this later

First, let’s set the Cake class property to the protected access modifier so that it is available only for the NewCake class, and add new fields and properties for the NewCake class. Let them be the AmountOfSpices and AmountOfSugar fields, and set it to protected, we will call these fields using methods

Now you have to correct the error related to the constructor, let’s add something like this to the NewCake class:

public NewCake(double AmountOfSalt, int BakingTime, int NumberOfEggs, int HowMuchMilk, int HowMuchFlour, double AmountOfSugar, double AmountOfSpices):base( AmountOfSalt, BakingTime, NumberOfEggs, HowMuchMilk, HowMuchFlour)
{
    amountOfSugar = AmountOfSugar;
    amountOfSpices = AmountOfSpices;
}

Already explaining what is it with the constructor, is not subjected to inheritance in the base class, in brackets next to the NewCake class name we must provide all arguments from the base class as well as from the inheriting class and in parentheses with the word “base” we must specify all variables from the constructor from the base class

Now the Cake and NewCake classes look like this:

//Cake class
 
class Cake
{
    private double _AmountOfSalt;
    public double AmountOfSalt
    {
        get
        {
            if (_AmountOfSalt < 0 || _AmountOfSalt > 50)
                return 0;
            else
                return _AmountOfSalt;
        }
        set
        {
            if (value < 0 || value > 50)
                Console.WriteLine("You gave the wrong amount of salt!");
            else
                _AmountOfSalt = value;
        }
    }
 
    private int _BakingTime;
    public int BakingTime
    {
        get
        {
            if (_BakingTime < 0 || _BakingTime > 100)
                return 0;
            else
                return _BakingTime;
        }
        set
        {
            if (value < 0 || value > 100)
                Console.WriteLine("You have entered the wrong baking time!");
            else
                _BakingTime = value;
        }
    }
 
    private int _NumberOfEggs;
    public int NumberOfEggs
    {
        get
        {
            if (_NumberOfEggs< 0 || _NumberOfEggs> 50)
                return 0;
            else
                return _NumberOfEggs;
        }
        set
        {
            if (value < 0 || value > 50)
                Console.WriteLine("You have given the wrong amount of eggs!");
            else
                _NumberOfEggs = value;
        }
    }
 
    private int _HowMuchMilk;
    public int HowMuchMilk
    {
        get
        {
            if (HowMuchMilk< 0 || HowMuchMilk> 50)
                return 0;
            else
                return HowMuchMilk;
        }
        set
        {
            if (value < 0 || value > 50)
                Console.WriteLine("You gave the wrong amount of milk!");
            else
                HowMuchMilk = value;
        }
    }
 
    private static int _HowMuchFlour;
    public int HowMuchFlour
    {
        get
        {
            if (_HowMuchFlour< 0 || _HowMuchFlour> 50)
                return 0;
            else
                return _HowMuchFlour;
        }
        set
        {
            if (value < 0 || value > 50)
                Console.WriteLine("You have given the wrong amount of flour!");
            else
                _HowMuchFlour = value;
        }
    }
 
    public Cake(double AmountOfSalt, int BakingTime, int NumberOfEggs, int HowMuchMilk, int HowMuchFlour)
    {  
        _AmountOfSalt = AmountOfSalt;
        _BakingTime = BakingTime;
        _NumberOfEggs = NumberOfEggs;
        _HowMuchMilk = HowMuchMilk;
        _HowMuchFlour = HowMuchFlour;
    }
 
    public void PourSalt()
    {
        Console.WriteLine("Pour " + _AmoutOfSalt + "g salt");
    }
    public string BakingCake(int time) 
    { 
       _bakingtime = time; 
       string text = "I change the baking time dough on " + time.ToString() + "min"; 
       return text;
    }
    public static void PourFlour()
    {
        Console.WriteLine("Pour " + _HowMauchFlour + "g Flour");
    }
}
 
// NewCake class
 
class NewCake : Cake
{
     private double _amountOfSugar;
     protected double AmountOfSugar
     {
         get
         {
             if (_amountOfSugar < 0 || _amountOfSugar > 50)
                 return 0;
              else
                 return _amountOfSugar;
         }
         set
         {
             if (value < 0 || value > 50)
                 Console.WriteLine("You have given the wrong amount of salt!");
             else
                 _amountOfSugar = value;
         }
     }
 
     private double _amountOfSpices;
     protected double AmountOfSpices
     {
         get
         {
             if (_amountOfSpices < 0 ||  _amountOfSpices > 50)
                 return 0;
             else
                 return _amountOfSpices;
         }
         set
         {
             if (value < 0 || value > 50)
                 Console.WriteLine("You have given the wrong amount of spices!");
             else
                 _amountOfSpices = value;
         }
     }
        
     public NewCake(double AmountOfSalt, int BakingTime, int NumberOfEggs, int HowMuchMilk, int HowMuchFlour, double AmountOfSugar, double AmountOfSpices):base(AmountOfSalt, BakingTime, NumberOfEggs, HowMuchMilk, HowMuchFlour)
     {
         _amountOfSugar = AmountOfSugar;
         _amountOfSpices = AmountOfSpices;
     }
}

We can also extend existing methods from the base class, but in order to do this we must add before the type of function that we want to extend the “virtual” word in the base class and in the inheriting class or derivative we adding word “override”, I have expanded the method from the Cake class like this:

//Cake class
 
public virtual void PourSalt()
{
    Console.WriteLine("Pour " + _AmoutOfSalt + "g salt");
}
 
//NewCake class
 
public override void PourSalt()
{
    base.PourSalt();
    Console.WriteLine("And some sugar " + _AmoutOfSalt + "g salt");
}

line base.PourSalt(); I call this method from the base class and I have extended this method in the NewCake class with such a line “Console.WriteLine (” And some sugar “+ _ AmountOfSugar +” g “);”

I mentioned earlier in previous lessons about the access modifier with the words “sealed”, when you use it in some class, it will not be inherited, so it looks like:

sealed class Cake

When you use it, the mass of errors will appear.

This content also you can find on my blog http://devman.pl/csharplan/c-language-11-inheritance/

If you recognise it as useful, share it with others so that others can also use it.

Leave upvote and follow and wait for next articles :) .

I think I’ve explained the basic aspects of inheritance well, that’s it, see you!

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63188.04
ETH 2570.49
USDT 1.00
SBD 2.79