Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 92355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:10:26+00:00 2026-05-10T23:10:26+00:00

I was of the opinion that virtualization doesnt work in the super class constructor

  • 0

I was of the opinion that virtualization doesnt work in the super class constructor as per the design of OOP. For example, consider the following C# code.

using System; namespace Problem {     public class BaseClass      {         public BaseClass()          {             Console.WriteLine('Hello, World!');             this.PrintRandom();         }         public virtual void PrintRandom()          {             Console.WriteLine('0');         }     }      public class Descendent : BaseClass      {         private Random randomValue;         public Descendent()          {             Console.WriteLine('Bonjour, Monde!');             randomValue = new Random();         }         public override void PrintRandom()          {             Console.WriteLine(randomValue.NextDouble().ToString());         }          public static void Main()          {             Descendent obj = new Descendent();             obj.PrintRandom();             Console.ReadLine();         }     } } 

This code breaks because when the object of Descendent is made, it calls the base class constructor and we have a virtual method call in Base Class constructor which in turn calls the Derived class’s method and hence, it crashes since randomValue is not intialized by that time.

A similar code works in C++ because the call to PrintRandom is not routed to the derived class since IMO, the order in C++ is something like:

1. call for base class constructor
2. Update V – Table for this class
3. call the constructor code

My Question is that firstly whether I am right that as per OOP principles, virtualization shouldn’t/doesn’t work in the super class constructor and secondly if I am right, then why the behavior is different in all .NET languages ( I have tested it with C#, VB.NET and MC++)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:10:27+00:00Added an answer on May 10, 2026 at 11:10 pm

    In native C++, the program works as expected: you get the call to the base class version of the virtual function within the base class constructor. At the time of the constructor call, only the base class and its virtual functions exist, so you get the lowest-level version of the virtual function defined at the time. This does not imply that virtualization cannot be used, you just won’t get the subclass versions of virtual methods in the constructors of base classes (which is why it’s not recommended).

    Obviously, as you can see, managed code works differently, because (iirc) the entire object is built before the constructors are called, and thus you get the subclass virtual function before the subclass constructor. This is a documented difference between the behaviors of the languages, but should be consistent across .NET languages (since they all compile to the same IL).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems to be the general opinion that tables should not be used for
I have long been of the opinion that the time and resources it would
I've encountered lately some opinions saying that Object Oriented design/programming should not always be
It is my opinion that every language was created for a specific purpose. What
I heard opinion that it make no sense to create indexes when field has
I've always been of the opinion that large switch statements are a symptom of
It seems to be a mainstream opinion that assembly programming takes longer and is
Is there a name for the software design pattern that involves MVC with domain
I'm an avid Emacs user and love shell-mode that in my opinion brings the
I just wanted some opinions from people that have run Selenium ( http://selenium.openqa.org )

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.