Rank: Member Groups: Member
Joined: 3/8/2008 Posts: 5
|
29. What is the order of destructors called in a polymorphism hierarchy?
Ans.Destructors are called in reverse order of constructors. First destructor of most derived class is called followed by its parent’s destructor and so on till the topmost class in the hierarchy.
You don’t have control over when the first destructor will be called, since it is determined by the garbage collector. Sometime after the object goes out of scope GC calls the destructor, then its parent’s destructor and so on.
When a program terminates definitely all object’s destructors are called.
30. What is a virtual method?
Ans.In C#, virtual keyword can be used to mark a property or method to make it overrideable. Such methods/properties are called virtual methods/properties.By default, methods and properties in C# are non-virtual.
31. Is it possible to Override Private Virtual methods?
No, First of all you cannot declare a method as ‘private virtual’.
32. Can I call a virtual method from a constructor/destructor?
Yes, but it’s generally not a good idea. The mechanics of object construction in .NET are quite different from C++, and this affects virtual method calls in constructors.C++ constructs objects from base to derived, so when the base constructor is executing the object is effectively a base object, and virtual method calls are routed to the base class implementation. By contrast, in .NET the derived constructor is executed first, which means the object is always a derived object and virtual method calls are always routed to the derived implementation. (Note that the C# compiler inserts a call to the base class constructor at the start of the derived constructor, thus preserving standard OO semantics by creating the illusion that the base constructor is executed first.)The same issue arises when calling virtual methods from C# destructors. A virtual method call in a base destructor will be routed to the derived implementation.
33. How do I declare a pure virtual function in C#?
Use the abstract modifier on the method. The class must also be marked as abstract (naturally). Note that abstract methods cannot have an implementation (unlike pure virtual C++ methods).
34. Are all methods virtual in C#?
No. Like C++, methods are non-virtual by default, but can be marked as virtual.
|