Inheritance

lional

Well-known member
Joined
Nov 29, 2018
Messages
60
Programming Experience
Beginner
Morning

If I have a parent class that stores generic personal data, and two child classes called Employee and Student. The parent class doesn't have any methods because the person is added either via the Employee or Student class, the parent class is there so that data is not repeated, should the parent class just be an interface and let the child classes inherit the interface, and should the parent class be a class / interface if it does not actually contain any methods, or is my structure incorrect.

Have a great day everyone
 
For the record, the correct terminology for .NET classes is "base" and "derived", rather than "parent" and "child".

If you have no need to create instances of your base type directly and there's no actual implementation in it then it may well be more appropriate to use an interface rather than a base class. You can still add methods to interfaces if they will have the same name and same purpose in implementing types, then provide the implementation in the concrete classes. I believe that you can even provide default implementations these days, although that's not something I've had cause to do so I'm not quite sure what all the implications are.
 
Also don't forget about abstract base classes...

For me the issue is if I need to add another property (or method) to the interface, how many classes will I have to touch to downstream, vs. just adding to the abstract base class vs. using the new C# feature of default implementations for interfaces.
 
Beyond software maintenance, there are also interesting implications for performance with regards to RAM and CPU given these possible implementations:
C#:
namespace RealPOCO
{
    abstract class Person
    {
        public string FirstName;
        public string LastName;
        public DateTime BirthDate;
    }

    class Student : Person
    {
        public string StudentId;
    }

    class Employee : Person
    {
        public DateTime HireDate;
    }
}

namespace POCO
{
    abstract class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime BirthDate { get; set; }
    }

    class Student : Person
    {
        public string StudentId { get; set; }
    }

    class Employee : Person
    {
        public DateTime HireDate { get; set; }
    }
}

namespace OverridablePOCO
{
    abstract class Person
    {
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual DateTime BirthDate { get; set; }
    }

    class Student : Person
    {
        public string StudentId { get; set; }
    }

    class Employee : Person
    {
        public DateTime HireDate { get; set; }
    }
}

namespace PoorMansInterface
{
    abstract class Person
    {
        public abstract string FirstName { get; set; }
        public abstract string LastName { get; set; }
        public abstract DateTime BirthDate { get; set; }
    }

    class Student : Person
    {
        public override string FirstName { get; set; }
        public override string LastName { get; set; }
        public override DateTime BirthDate { get; set; }

        public string StudentId { get; set; }
    }

    class Employee : Person
    {
        public override string FirstName { get; set; }
        public override string LastName { get; set; }
        public override DateTime BirthDate { get; set; }

        public DateTime HireDate { get; set; }
    }
}

namespace UseInterface
{
    interface IPerson
    {
        string FirstName { get; set; }
        string LastName { get; set; }
        DateTime BirthDate { get; set; }
    }

    class Student : IPerson
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime BirthDate { get; set; }

        public string StudentId { get; set; }
    }

    class Employee : IPerson
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime BirthDate { get; set; }

        public DateTime HireDate { get; set; }
    }
}

namespace UseInterfaceWithBaseClass
{
    interface IPerson
    {
        string FirstName { get; set; }
        string LastName { get; set; }
        DateTime BirthDate { get; set; }
    }

    abstract class BasePerson : IPerson
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime BirthDate { get; set; }
    }

    class Student : BasePerson
    {
        public string StudentId { get; set; }
    }

    class Employee : BasePerson
    {
        public DateTime HireDate { get; set; }
    }
}
 

Latest posts

Back
Top Bottom