Hi folks here I am shareing the Important Interview questions for more then 1 yr experience MS.NET professional's.
1. What is the difference between abstract class and interface?
i.An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.
ii.An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation.
iii.An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.
iv.A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
v.Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
vi.Abstract classes are faster than interfaces.
a)A class may inherit only one abstract class, but may implement multiple number of Interfaces. Say a class named Car needs to inherit some basic features of a vehicle, it may inherit from an Abstract class named Vehicle. A car may be of any kind, it may be a vintage car, a sedan, a coupe, or a racing car. For these kind of requirements, say a car needs to have only two seats (means it is a coupe), then the class Car needs to implement a member field from an interface, that we make, say ICoupe.
b)Members of an abstract class may have any access modifier, but members of an interface are public by default, and cant have any other access modifier.
c)Abstract class methods may OR may not have an implementation, while methods in an Interface only have a definition, no implementation.
2Understanding Constructors in C#
Constructors are used for initializing the members of a class whenever an object is created with the default values for initialization.
If a class is not defined with the constructor then the CLR (Common Language Runtime) will provide an implicit constructor which is called as Default Constructor.
A class can have any number of constructors provided they vary with the number of arguments that are passed, which is they should have different signatures.
- Constructors do not return a value.
- Constructors can be overloaded.
- If a class is defined with static and Non-static constructors then the privilege will be given to the Non-static constructors.
The following are the access modifiers for constructors,
Public: A constructor that is defined as public will be called whenever a class is instantiated.
Protected: A constructor is defined as protected in such cases where the base class will initialize on its own whenever derived types of it are created.
Private: A constructor is defined as private in such cases whenever a class which contains only static members has to be accessed will avoid the creation of the object for the class.
Internal: An internal constructor can be used to limit concrete implementations of the abstract class to the assembly defining the class. A class containing an internal constructor cannot be instantiated outside of the assembly.
External: When a constructor is declared using an extern modifier, the constructor is said to be an external constructor.
Types of Constructors
i) Static: Used for initializing only the static members of the class. These will be invoked for the very first time the class is being loaded on the memory. They cannot accept any arguments. Static Constructors cannot have any access modifiers.
Syntax ----
Static ClassName()
{
//Initialization statements;
}
ii) Non-Static: are used for initializing the Non-Static and Static members of a class. These will be invoked everytime a new object is defined for a class.
Syntax ---
Public ClassName([argsInfo])
{
//Initialization statements;
}
Example :
Using System;
Class SampleConstructor
{
public static int s;
public int ns;
static SampleConstructor()
{
s = 10;
//ns = 20; --- Error cannot be assigned like this
}
public SampleConstructor()
{
ns=100;
s=200;
}
}
Class UseSampleConstructor
{
public static void Main()
{
SampleConstructor sc = new SampleConstructor();
Console.WriteLine(“{0},{1}”,sc.s, SampleConstructor.s, sc.ns);
} l
} l
Error (Cannot call like this)
If you observe in the above example the static variable `ns' cannot be assigned a value in the static Constructor as it is a Non-static member of the class and a static constructor cannot initialize a Non-Static member.
Also as you see in the above code the Non-static constructor initializing the values for both the static and Non-Static members of class, when we say `sc' we are creating an object for the class and using the object we do the call the methods, it is not valid to call a static member of class using the object of the class we have to call it through the method name/Constructor name through the member will be invoked.
Interview Questions
Subscribe to:
Posts (Atom)





