Friday, March 27, 2009

OOPs Interview Questions Part II

Question: What is the difference between Shared and Static?
Answer: They both mean the same. Shared is used in VB.NET. Static is used in C#.
When the static keyword is used to declare a class, the member in context must be directly invoked from the class, rather than from the instance. Consider the following example 
//Consider writing the following line of code...
Console obj = new Console(); 
obj.Writeline("Vishal likes static members"); //This line does'nt print

//This does'nt work, because WriteLine is a static method defined in the class Console
//The Console class is a static class 
To use static members, give a reference to the exact class, as an instance in this case won't work. 
To make this work, write...
Console.Writeline("Vishal likes static members"); 
To work with members of static classes, no need to create their instances.
Static Member - A class member declared with the keyword static is a static member. A static member is owned by the class, not by its instances (objects of the class). 
Note that static members are actually class members, while non-static members are instance members (means they are owned by the instances). Both in C# & VB.NET, we may create static/shared events, properties, fields and functions. 
Note Indexers in C# cannot be declared static.
Note Static member functions cannot access non-static members directly.
Virtual - If a base class method is to be overriden, it is defined using the keyword virtual (otherwise the sealed keyword is used to prevent overriding). 
Note that the class member method may be overriden even if the virtual keyword is not used, but its usage makes the code more transparent & meaningful. In VB.NET, we may use the overridable keyword for this purpose. 

When the override keyword is used to override the virtual method, in a scenario where the base class method is required in a child class along with the overriden method, then the base keyword may be used to access the parent class member. The following code example will make the usage more clear. 
public class Employee
{
public virtual void SetBasic(float money) //This method may be overriden
{ Basic += money; }
}


public class Manager : Employee
{
public override void SetBasic(float money) //This method is being overriden
{
float managerIncentive = 10000;
base.SetSalary(money + managerIncentive); //Calling base class method
}
}
Question: Explain overridable, overrides, notoverridable,mustoverride 
Answer: These keywords are used in VB.NET.

Overridable -The Overridable keyword is used when defining a property or method of an inherited class, as overridable by the inheriting class. 

Overides - The Overides keyword allows the inheriting class to disregard the property or method of the inherited class and implements ts own code. 

NotOverridable - The NotOverridable keyword explicitly declares a property or method as not overridable by an inheriting class, and all properties are "not overridable" by default. The only real advantage to using this keyword is to make your code more readable. 

MustOverride - The MustOverride keyword forces the inheriting class to implement its own code for the property or method. 
Question: What is the difference between Overriding and Shadowing?
Answer: Both Overriding and Shadowing are ways to alter the behaviour of members of a base class. Shadowing is a VB.NET concept. In C#, this concept is called Hiding, though there is a difference between the two.

When we do shadowing, we provide a new implementation to the base class member without overriding it. We may shadow a base class member in a derived class, by using the keyword shadows. The access level, return type, and the signature (means the datatypes of the arguments passed & the order of the types) of the derived class members which are shadowed, may differ from the base class. 

In C#, we may achieve shadowing using the keyword new. However, when Hiding in C#, the access level, the signature, return type of the derived class must be same as the base class. 

Overriding is the concept of providing a new implementation of derived class member as compared to its based class. In VB.NET, we do overriding using the overrides keyword, while in C#, overriding is achieved using the override keyword. For a class member to be overridable, we use the keyword virtual while defining it (in C#), and we use the keyword overridable (in VB.NET), though if we leave out specifying the overridable keyword, the member is overridable by default. 

Question: What is a static constructor? 
Answer: Static Constructor - It is a special type of constructor, introduced with C#. It gets called before the creation of the first object of a class(probably at the time of loading an assembly). See example below. 
Example:
public class SomeClass()
{
static SomeClass()
{
//Static members may be accessed from here
//Code for Initialization
}
}
While creating a static constructor, a few things need to be kept in mind:
* There is no access modifier require to define a static constructor
* There may be only one static constructor in a class
* The static constructor may not have any parameters
* This constructor may only access the static members of the class
* We may create more than one static constructor for a class

Question: Can a class be created without a constructor? 
Answer: No. In case we dont define the constructor, the class will access the no-argument constructor from its base class. The compiler will make this happen during compilation. 

No comments: