In a multilevel hierarchy how are the constructors are called

A) TopDown
B) BottomUp
C) None

Showing Answers 1 - 27 of 27 Answers

Sean Paul

  • Apr 1st, 2006
 

Bottom Up,  The question is how are they called, not order in which they are they executed. i.e. first the derived class' constructer will be called which in turn will call the parent class' constructor

Ashish

  • Feb 5th, 2007
 

It will always be topdown: class a { public a() { Console.WriteLine("Class a"); Console.ReadLine(); } } class b:a { public b() { Console.WriteLine("Class b"); Console.ReadLine(); } } class c : b { public c() { Console.WriteLine("Class c"); Console.ReadLine(); } static void Main(string[] args) { c obj = new c(); } }It will give the result:Class aClass bClass C

LordAlex

  • Nov 4th, 2011
 

An object of derived type is constructed from the base class to the derived class by calling the constructors for each class in order.

Rational:Each class's constructor can use base class members in the class constructor.

  Was this answer useful?  Yes

Philc137

  • Nov 9th, 2011
 

Bottom up.

A parent class has no reference to the child class, it's the other way around. The code calls the child class, which then calls the parent class, and so on up. Then the constructors are executed top down.

  Was this answer useful?  Yes

Damian

  • Apr 5th, 2014
 

Bottomup

Code
  1. using System;

  2.  

  3. public class A // This is the base class.

  4. {

  5.     public A()

  6.     {

  7.         // Executes some code in the constructor.

  8.         Console.WriteLine("Base constructor A()");

  9.     }

  10. }

  11.  

  12. public class B : A // This class derives from the previous class.

  13. {

  14.     public B()

  15.     {

  16.         // The base constructor is called first.

  17.         // ... Then this code is executed.

  18.         Console.WriteLine("Derived constructor B()");

  19.     }

  20. }

  21.  

  22. class Program

  23. {

  24.     static void Main()

  25.     {

  26.         // Create a new instance of class A, which is the base class.

  27.         // ... Then create an instance of B, which executes the base constructor.

  28.         var a = new A();

  29.         var b = new B();

  30.     }

  31. }

  Was this answer useful?  Yes

Jimuta Bahan Sahu

  • May 27th, 2014
 

A) topdown

  Was this answer useful?  Yes

narendra

  • Aug 14th, 2014
 

Code
  1.  class Program

  2.     {

  3.  

  4.         public class BaseClass

  5.         {

  6.             public BaseClass()

  7.             {

  8.                 Console.WriteLine("Base class constructor");

  9.                 Console.ReadLine();

  10.             }

  11.         }

  12.  

  13.         public class ChildClass : BaseClass

  14.         {

  15.             public ChildClass()

  16.             {

  17.                 Console.WriteLine("Child class constructor");

  18.                 Console.ReadLine();

  19.             }

  20.         }

  21.  

  22.         static void Main()

  23.         {

  24.             ChildClass ch = new ChildClass();

  25.         }

  26.     }

  27.  

  28. O/P : Base class constructor

  29.  

  30. Child class constructor

  Was this answer useful?  Yes

Sunil

  • Aug 31st, 2014
 

TOPDOWN is the correct answer.

Always base class constructor is called before a sub-class constructor gets called.

So, if a class is higher in the class hierarchy, then it will always get called before any sub-class that is lower down in hierarchy.

In sample code below, if we instantiate SClass then automatically the constructor of BClass is called before the SClass constructor. BClass is parent of sub-class SClass, so the higher class constructor gets called first.

Code
  1. class BClass

  2. {

  3.   public int Num;

  4.    public BClass()

  5.    {

  6.       Num  = 20;

  7.    }

  8. }

  9.  

  10. class SClass : BClass

  11. {

  12.    public string MyName;

  13.    public SClass()

  14.     {

  15.        this.Num =30;

  16.        MyName = "Sunil";

  17.     }

  18.  

  19. }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions