I’m needing some advice on structuring my classes for an application which models 6 different but similar types. The 6 types have some properties and methods they will all share. There are also properties and methods that 3, 4, or 5 of them will share but would have no meaning to the others.
Currently, I have an abstract base class with:
- properties and methods that all of the subclasses will use, already implemented.
- methods that some of the subclasses will use marked as abstract, to be implemented in the subclasses.
- properties that some of the subclasses will use marked as nullable so that subclasses in which the property would not be used it could be null.
This just doesn’t seem like the best solution though. I feel like I have provided enough details, but if this doesn’t make sense just let me know and I will try to provide a better description.
EDIT — adding code example
public abstract class BaseClass
{
//all classes inheriting from BaseClass would use these properties
public string Property1{ get; set; }
public string Property2{ get; set; }
public void Method1() {
//all classes inheriting from BaseClass would use this method
}
//more than one class (but not all classes)
// inheriting from BaseClass would use these properties
public int? Property3 { get; set; }
public bool? Propert4 { get; set; }
public abstract void Method2() {
//more than one class (but not all classes)
// inheriting from BaseClass would use this method
//those not using this method would have empty getters and setters maybe?
}
}
There is little detail, but it does sound like you’d be better off having a separate “layer” in your class hierarchy.
Classes 3, 4, and 5 could derive from a separate abstract class, which in turned inherited your “base” class. This would eliminate the need for classes 1 and 2 to have the “unused” methods and properties in them.
Basically, the “properties that some of the subclasses will use marked as nullable so that subclasses in which the property would not be used it could be null” would not be in the base class, but rather added to a subclass, which in turn was subclasses for 3, 4, and 5.