Search code examples
c#visual-studioinheritanceinterfaceradix

C# OOP practice confusion regarding inheritance


I am reading one of my C# programming course material. I understand the code itself, but I don't quite understand why the tutor organises the class this way.

namespace GradeBook
{



public delegate void GradeAddedDelegate(object sender, EventArgs args);
public class NamedObject 
{
    public NamedObject(string name)
    {
        Name = name;
    }

    public string Name
    {
        get;
        set;
    }
}

public interface IBook
{
    void AddGrade(double grade);
    Statistics GetStatistics();
    string Name { get; }
    event GradeAddedDelegate GradeAdded;
}

public abstract class Book : NamedObject, IBook
{
    public Book(string name) : base(name)
    {
    }

    public abstract event GradeAddedDelegate GradeAdded;
    public abstract void AddGrade(double grade);
    public abstract Statistics GetStatistics();
}

public class DiskBook : Book
{
    public DiskBook(string name) : base(name)
    {
    }

    public override event GradeAddedDelegate GradeAdded;

    public override void AddGrade(double grade)
    {
        using(var writer = File.AppendText($"{Name}.txt"))
        {                
            writer.WriteLine(grade);
            if(GradeAdded != null)
            {
                GradeAdded(this, new EventArgs());
            }
        }
    }

    public override Statistics GetStatistics()
    {
        var result = new Statistics();

        using(var reader = File.OpenText($"{Name}.txt"))
        {
            var line = reader.ReadLine();
            while(line != null)
            {
                var number = double.Parse(line);
                result.Add(number);
                line = reader.ReadLine();
            }
        }

        return result;
    }
}

The code snippet above is a GradeBook for normal students.

  1. class NameObject defines a property Name and an initializer, which is easy to understand.
  2. He put all method in an interface IBook, and there is another class called Book, which is inherited from NameObject class and interface IBook. I am assuming he deliberately separate properties and methods, event. Is this correct?
  3. In the Book class, the initializer public Book(string name) : base(name). I know the base is defining a reference type. I am getting a bit confused with the name parameter. Does it refer to the property Name in NameObject?
  4. DiskBook is a subclass of Book, the initializer public Book(string name) : base(name) is passing a reference name as the last one. Does the name here refers to the name property in NameObject as well?

Thanks in advance.


Solution

    1. Logically, a lot of things could be a NamedObject. He's saying a Book is an IBook, and also a NamedObject. Imagine adding a Person class. That Person would be a NamedObject, but not an IBook. So you can reuse the base class without tying it to a Book

    2. using : base(name) says call the constructor on the base class with this parameter. So you can be sure that NamedObject's constructor will be called with the string value, setting the Name property. Excluding the : base(name) from the Book constructor will cause a compiler error, as the base class does not have a parameterless constructor.

    3. In the same manner, DiskBook is using : base(name) to call the constructor on Book, which in turn calls the constructor on NamedObject.