Back to the basics…

While watching this course from K. Scott Allen I’ve re-learned some things about Object Oriented Programming, namely this ..

If you have a class…

public class GradeBook
{
    public List<int> ListOfNumbers { get; set; }

    public int ReturnLowest()
    {
        int lowest = int.MaxValue;

        foreach (int number in ListOfNumbers)
        {
            lowest = Math.Min(lowest, number);
        }

        return lowest;
    }
}

and a derived class…

public class AugmentedGradeBook : GradeBook
{
    public int ReturnLowest()
    {
        int lowest = int.MaxValue;
        foreach (var number in ListOfNumbers)
        {
            lowest = Math.Min(lowest, number);
        }

        ListOfNumbers.Remove(lowest);

        return base.ReturnLowest();
    }
}

The c# compiler will choose the method to run based on the type of the variable.

[Theory]
[InlineData(new int[] { 1, 2, 3, 4, 5 })]
[InlineData(new int[] { 5, 4, 3, 2, 1 })]
[InlineData(new int[] { 5, 4, 1, 3, 2 })]
public void ReturnsLowestBaseClass(int[] listInts)
{
    GradeBook gradeBook = new AugmentedGradeBook();
    gradeBook.ListOfNumbers = listInts.ToList();

    var lowest = gradeBook.ReturnLowest();

    Assert.Equal(1, lowest);
}

Since we have a variable of type GradeBook gradeBook it will execute the method GradeBook.ReturnLowest();
On the other hand, if we have a variable of type AugmentedGradeBook gradeBook

[Theory]
[InlineData(new int[] { 1, 2, 3, 4, 5 })]
[InlineData(new int[] { 5, 4, 3, 2, 1 })]
[InlineData(new int[] { 5, 4, 1, 3, 2 })]
public void ReturnsLowestOwnClass(int[] listInts)
{
    AugmentedGradeBook gradeBook = new AugmentedGradeBook();
    gradeBook.ListOfNumbers = listInts.ToList();

    var lowest = gradeBook.ReturnLowest();

    Assert.Equal(2, lowest);
}

it will execute AugmentedGradeBook.ReturnLowest();

To achieve polymorphism we can change the class GradeBook to use a virtual method and then override the behavior in another class.

public class GradeBook
{
    public List<int> ListOfNumbers { get; set; }

    public virtual int ReturnLowest()
    {
        int lowest = int.MaxValue;

        foreach (int number in ListOfNumbers)
        {
            lowest = Math.Min(lowest, number);
        }

        return lowest;
    }
}

and the new derived class

public class AugmentedVirtualGradeBook : GradeBook
{
    public override int ReturnLowest()
    {
        int lowest = int.MaxValue;
        foreach (var number in ListOfNumbers)
        {
           lowest = Math.Min(lowest, number);
        }

        ListOfNumbers.Remove(lowest);

        return base.ReturnLowest();
    }
}

this way we can have a variable of type GradeBook gradeBook and the executed method will be AugmentedVirtualGradeBook.ReturnLowest

[Theory]
[InlineData(new int[] { 1, 2, 3, 4, 5 })]
[InlineData(new int[] { 5, 4, 3, 2, 1 })]
[InlineData(new int[] { 5, 4, 1, 3, 2 })]
public void ReturnsLowestVirtualAugmentedClass(int[] listInts)
{
    GradeBook gradeBook = new AugmentedVirtualGradeBook();
    gradeBook.ListOfNumbers = listInts.ToList();

    var lowest = gradeBook.ReturnLowest();

    Assert.Equal(2, lowest);
}

Hope it helps…
Cheers!

Advertisements
This entry was posted in c#, Object Oriented Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s