Base class property inheriting from base

I am reviewing the code and came across a situation where an abstract class inherits from an abstract base class that has the property of the inheriting class.

I can't debug the code at the moment to see how it will work, but I'm pretty sure it has been in production for years.

As such it is a mystery to me how the following logic will work step by step.

testator:

public class Inheritor : Base {
  ...
  properties
  ...
}

      

Base:

public class Base {
  public Inheritor { get; set; }
  ...
  other properties
  ...
}

      

How is this possible?

+3


source to share


2 answers


An example in which it hopefully makes sense at a conceptual level:

public class Boss : Employee {
  ...
  properties
  ...
}

public class Employee {
  public Boss { get; set; }
  ...
  other properties
  ...
}

      



There can be many kinds of employees, but each one can have a boss (which is a special kind of employee). Recall that properties of non-primitive types are references, so not the case when it Employee

contains a Boss

, which (since it is also Employee

) contains a Boss

, etc. Employee

may refer to Boss

, which may refer to Boss

, etc. At some point, it stops at Boss

, which does not have Boss

(link null

), or it goes in a circle.

+6


source


I think you can turn off the relational concept of is-a versus has-a , where you think it has - somehow limited in the same way as is-a. Inheritor

is Base

, and it would be very strange if the converse were true: if a is Base

also Inheritor

. But there is no reason that can not be Base

a Inheritor

.



+3


source







All Articles