scitools.com

← Metric catalog

Max Inheritance Tree is one of the source-code metrics Understand computes across 17+ languages — browse them in the GUI, export and track them, or script them with the Python API.

Max Inheritance Tree

API ID: MaxInheritanceTree
Also Known As: Depth of Inheritance Tree (DIT)
Languages: C#, C++, Java, Pascal, Python, Web
Targets: Classes

Maximum depth of class in inheritance tree.

The depth of a class within the inheritance hierarchy is the maximum number of nodes from the class node to the root of the inheritance tree. The root node has a DIT of 0. The deeper within the hierarchy, the more methods the class can inherit, increasing its complexity. For multiple inheritance, the depth is 1 plus the maximum depth among all of the class's direct base classes, not the sum.

For example:


class Vehicle {
public:
  Vehicle() {}
};

class Wheeled : public Vehicle {
public:
  Wheeled(int wheels) : mWheels(wheels) {}
  int wheels() { return mWheels; }
  void setWheels(int wheels) { mWheels = wheels; }
private:
  int mWheels;
};

class Car : public Wheeled {
public:
  Car() : Wheeled(4) {}
  Car(int wheels) : Wheeled(wheels) {}
};

class Boat : public Vehicle {
public:
  Boat() {}
  ~Boat() {}
};

class DualPurpose {
public:
  DualPurpose() {}
  char* purpose1() { return nullptr; }
  char* purpose2() { return nullptr; }
};

class BoatCar: private Car, public Boat, protected DualPurpose {
public:
  BoatCar() : Car(4), Boat(), mInWater(false), mColor("Blue") {}
  virtual int passengers() const { return 4; }

  static int numRegistered() { return sRegistered; }

  bool mInWater;

protected:

  void toggleInWater(bool inWater) { mInWater = inWater; }
  char * mColor;
  friend void init() {}

  static int sRegistered;
  static double calcSpeed(double distance, double time) {
    return distance/time;
  }

private:
  int mMaxPassengers;
  void travel() {}
};

class TourBoatCar : public BoatCar {};
class WildLifeTourBoatCar: public TourBoatCar {};

Vehicle is a root, so MaxInheritanceTree = 0. Wheeled and Boat derive directly from Vehicle, so each is 1. Car derives from Wheeled, so it's 2. DualPurpose has no base class, so it's also 0. BoatCar multiply inherits Car (depth 2), Boat (depth 1), and DualPurpose (depth 0); taking the maximum of those and adding 1 gives BoatCar = 3. That continues down the chain: TourBoatCar = 4 and WildLifeTourBoatCar = 5.

See Base Classes for the number of direct base classes this depth is computed from.

Also known as Chidamber & Kemerer - Depth of Inheritance Tree (DIT).


Targets By Language: Configuration: