Demonstrate when constructors are called. : Class hierarchy « Class « C# / CSharp Tutorial






The general form of calling base constructor:

derived-constructor(parameter-list) : base(arg-list) {
    // body of constructor
}
using System; 
 
// Create a base class. 
class BaseClass { 
  public BaseClass() {  
    Console.WriteLine("Constructing BaseClass."); 
  } 
} 
 
// Create a class derived from BaseClass. 
class DerivedClass : BaseClass { 
  public DerivedClass() { 
    Console.WriteLine("Constructing DerivedClass."); 
  } 
} 
 
// Create a class derived from DerivedClass. 
class DerivedDerivedClass : DerivedClass { 
  public DerivedDerivedClass() { 
    Console.WriteLine("Constructing DerivedDerivedClass."); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    DerivedDerivedClass c = new DerivedDerivedClass(); 
  } 
}
Constructing BaseClass.
Constructing DerivedClass.
Constructing DerivedDerivedClass.








7.20.Class hierarchy
7.20.1.A simple class hierarchy.
7.20.2.A multilevel hierarchy.
7.20.3.Demonstrate when constructors are called.
7.20.4.Simple Inheritance
7.20.5.Inherited Implementation