Inheritence in C# RRS feed

  • Question

  • Dear all,

    I have a design advise on my classes based on the follwoing.

    Let say I have a main class wish is a Car()
    Then I have sub class which inherit from Car() which can be Mercedes(), Ferari, Chevrelet()
    Then I can have Cars which are using fuel or electric energie

    Based on that should I create new Class Energy(), then sub class inherited from it as Fuel() and Energy(), or should I better create those class inside a particular class of Car ?

    In fact the main point I try to identify is that after how many class hierarchy should we consider the class structure start to be complicated and need to be simplified ?

    Thanks for advise


    Monday, November 27, 2017 9:29 AM

All replies

  • Inheritance is for when some object is clearly a sub-type of another object in terms the properties and functionality that it has.

    So a Ferrari is a type of Car, because a Ferrari has all the same basic properties of any car (steering wheel, wheels, a chassis etc) and performs the same function (provides transportation from one location to another by way of wheeled locomotion). Plus it has additional features specific to a Ferrari.

    But an 'electric' is not a type of car in an object-oriented sense (despite the way we tend to say it language). It does not - on its own - have the same properties: An 'electric' does not have wheels. You cannot drive an 'electric' to work. The property of being of electric in fact could apply across a number of specific types of car across the board such as Mercedes, Ferraris etc.

    Instead, A car has a property representing the type of fuel which can be Petrol, Diesel, Electric, Hybrid, Hydrogen fuel-cell etc.

    Summary: I would say that if I had a Car class, I would also define an enum of type FuelSource. The car class could then have a property of type FuelSource that would define the type of fuel it uses. All classes that derive from Car (such as Ferrari) would inherit that property. (Other solutions may exist!).


    enum FuelSource {Petrol, Diesel, Electric};
    public class Car
       property FuelSource FuelType{get;set;}
    public class Ferrari: Car

    • Edited by RJP1973 Monday, November 27, 2017 1:47 PM
    Monday, November 27, 2017 1:46 PM
  • Thanks for your explanation.

    Still have a question, after how many nested class number we should consider that it start to be complicated to handle and need split or simplification ?


    Monday, November 27, 2017 3:16 PM
  • I think, from what I've read, that object inheritance should not be nested too deeply. But "too deeply" is going to be subjective.

    I couldn't give you a definitive number, but if you end up with more than 5 or 6 (?) levels then maybe its time to think about whether there is a better way of representing your model, typically with interfaces, sub-classes and so on.

    But it really depends on your own code and what you are modelling. If you find its getting too complicated to handle, then you've gone too deep!

    • Edited by RJP1973 Monday, November 27, 2017 3:21 PM
    • Proposed as answer by Fei HuModerator Wednesday, November 29, 2017 9:25 AM
    Monday, November 27, 2017 3:21 PM
  • So I guess there is no standard rules and depends on each way of doing for developpers


    Monday, November 27, 2017 3:37 PM