In a system for managing vocational training, I have a CourseBase
abstract class, which I decided on using in favour of an ICourse
interface because I'd prefer to avoid duplicating implementation code for all classes derived from the hypothetical, base Course
entity. Each course has a list if subjects, with any subject defined by a SubjectBase
abstract class. So, I have e.g.
public abstract class CourseBase : BaseObject
{
public IEnumerable<SubjectBase> Subjects
{
get { return new List<SubjectBase>(); }
}
}
public abstract class SubjectBase
{
public string Name { get; set; }
public string Description { get; set; }
public int ValidityPeriod { get; set; }
}
Now I want to add a concrete class, LocalCourse
, which contains a collection of LocalCourseSubject
objects, but because I'm not using an interface for CourseBase
, I lose out on covariance, and I need to hide the abstract base's Subjects
property with my new:
public class LocalCourse: CourseBase
{
public IEnumerable<LocalCourseSubject> Subjects
{
get { throw new NotImplementedException(); }
}
}
I'm sure I'm missing something very obvious here from an OO point of view, but the only solutions I can see are:
- Completely omit Subjects from the abstract base, and only add a specifically typed collection property to derived classes.
- Implement an interface such as
ISubjectCollectionOwner
in the abstract base as well as concrete classes.
Please excuse my dimness here, it's been a while since I've had the pleasure of encountering a design issue like this.