OOPS Interview Questions and Answers (133) - Page 2

What is Internal access modifier in C#?

The internal keyword is an access modifier for types and type members ie. we can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll). In other words, access is limited exclusively to classes defined within the current project assembly.

For more details see http://msdn.microsoft.com/en-us/library/7c5ka91b(VS.71).aspx
What is Protected Internal access modifier in C#?

Protected Internal is a access modifiers for the members (methods or functions) ie. you can't declare a class as protected internal explicitly. The members access is limited to the current assembly or types derived from the containing class.

Protected Internal means the method is accessible by anything that can access the protected method UNION with anything that can access the internal method.

For more details read http://haacked.com/archive/2007/10/29/what-does-protected-internal-mean.aspx
Default Access modifiers in C#?

An enum has default modifier as public

A class has default modifiers as Internal . It can declare members (methods etc) with following access modifiers:
protected internal

An interface has default modifier as public

A struct has default modifier as Internal and it can declare its members (methods etc) with following access modifiers:

A methods, fields, and properties has default access modifier as "Private" if no modifier is specified.
What is method overloading?

Method overloading allows us to write different version of the same method in a class or derived class. Compiler automatically select the most appropriate method based on the parameter supplied.

public class MultiplyNumbers

public int Multiply(int a, int b)
return a * b;

public int Multiply(int a, int b, int c)
return a*b*c;

To call the above method, you can use following code.
MultiplyNumbers mn = new MultiplyNumbers();

int number = mn.Multiply(2, 3) // result = 6
int number1 = mn.Multiply(2, 3, 4) // result = 24

You can't have a overload method with same number parameters but different return type. In order to create overload method, the return type must be the same and parameter type must be different or different in numbers.
What is Overriding?

Method overriding is a feature that allows to invoke functions (that have the same signatures) and that belong to different classes in the same hierarchy of inheritance using the base class reference. In C# it is done using keywords virtual and overrides .

For more information visit http://www.codeproject.com/KB/cs/cs_methodoverride.aspx
What is Method overloading?

Method overloading occurs when a class contains two methods with the same name, but different signatures.
What is Method Overriding? How to override a function in C#?

Use the override modifier to modify a method, a property, an indexer, or an event. An override method provides a new implementation of a member inherited from a base class. The method overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method.

You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.
Can we call a base class method without creating instance?

Yep. But ..

* Its possible If its a static method.

* Its possible by inheriting from that class also.

* Its possible from derived classes using base keyword.
In which cases you use override and new base?

Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.
Difference between new and override keyword?

Let me explain this through code.

using System;

using System.Data;
using System.Text;
using System.Windows.Forms;

namespace BaseDerive
public partial class Form1 : Form
public Form1()

private void Form1_Load(object sender, EventArgs e)

BaseClass b = new BaseClass();

DeriveClass d = new DeriveClass();

//Calls Base class function 1 as new keyword is used.
BaseClass bd = new DeriveClass();

//Calls Derived class function 2 as override keyword is used.
BaseClass bd2 = new DeriveClass();


public class BaseClass
public virtual void func1()
MessageBox.Show("Base Class function 1.");

public virtual void func2()
MessageBox.Show("Base Class function 2.");

public void func3()
MessageBox.Show("Base Class function 3.");

public class DeriveClass : BaseClass
public new void func1()
MessageBox.Show("Derieve Class fuction 1 used new keyword");

public override void func2()
MessageBox.Show("Derieve Class fuction 2 used override keyword");

public void func3()
MessageBox.Show("Derieve Class fuction 3 used override keyword");


This is a window application so all the code for calling the function through objects is written in Form_Load event.
As seen in above code, I have declared 2 classes. One works as a Base class and second is a derieve class derived from base class.

Now the difference is

new: hides the base class function.
Override: overrides the base class function.

BaseClass objB = new DeriveClass();

If we create object like above notation and make a call to any function which exists in base class and derive class both, then it will always make a call to function of base class. If we have overidden the method in derive class then it wlll call the derive class function.

For example…

objB.func1(); //Calls the base class function. (In case of new keyword)

objB.func2(); //Calls the derive class function. (Override)
objB.func3(); //Calls the base class function.(Same prototype in both the class.)

// This will throw a compile time error. (Casting is required.)
DeriveClass objB = new BaseClass(); 

//This will throw run time error. (Unable to cast)
DeriveClass objB = (DeriveClass) new BaseClass(); 

Hope this helps!!!!!
What is a private constructor? Where will you use it?

When you declare a Constructor with Private access modifier then it is called Private Constructor. We can use the private constructor in singleton pattern.

If you declare a Constructor as private then it doesn’t allow to create object for its derived class, i.e you loose inherent facility for that class.


Class A

// some code
Private Void A()
//Private Constructor

Class B:A

B obj = new B();// will give Compilation Error

Because Class A constructor declared as private hence its accessibility limit is to that class only, Class B can't access. When we create an object for Class B that constructor will call constructor A but class B have no rights to access the Class A constructor hence we will get compilation error.
Can we declare private class in a Namespace?

No. If you try to create a private class in a Namespace, Compiler will throw a compile time error “Namespace elements cannot be explicitly declared as private, protected, or protected internal”.

Reason: The message says it all. Classes can only be declared as private, protected or protected internal when declared as nested classes, other than that, it doesn't make sense to declare a class with a visibility that makes it unusable, even in the same module. Top level classes cannot be private, they are "internal" by default, and you can just make them public to make them visible from outside your DLL.
What is Polymorphism?

In OPP’S, polymorphism(Greek meaning “having multiple forms”) is the ablity of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a a function, or an object to have more than one forms.

In C# :
Parent classes may define and implement “virtual” methods(Which is done using the “virtual” keyword), and derived classes can override them(using the “override” keyword), which means they provide their own definition and implementation.At run-time, when user’s code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code when a method of the base class is called it executes the overriden method.
Regards Hefin Dsouza.
What Are Attributes in DotNet?

An Attribute is a declarative tag which can be used to provide information to the compiler about the behaviour of the C# elements such as classes and assemblies.
C# provides convenient technique that will handle tasks such as performing compile time operations , changing the behaviour of a method at runtime or maybe even handle unmanaged code.
C# Provides many Built-in Attributes

Some Popular ones are

- Obsolete
- DllImport
- Conditional
- WebMethod

and Many more.
Members please keep on posting more responses providing more In-Built attributes.

Regards Hefin Dsouza
What can you do to make class available for inheritance but you need to prevent it's method to come in inheritance chain?

Well, Declare a class with public access specifier and mark all it's method to private . As anything which is declared with sealed keyword cannot be inherited.
What's the Difference between Interface and Abstract Class

Abstract Class:
Have constructors.
Not necessarily for the class inheriting it to Implement all the Methods.
Doesn't Support Multiple Inheritance.

Where everything is Opposite in the Interfaces.
What are the various types of Constructors

Public : Accessible to All
Private: Those classes in which only static members are there and you don't want there objects to be created in any class.
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.
Intern: implementations of the abstract class to the assembly defining the class. A class containing an internal constructor cannot be instantiated outside of the assembly (Namespace).
and External
What are Constructors ?

Constructors are used for initializing the members of a class whenever an object is created with the default values for initialization.

If no constructor defined then the CLR 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
When to Use Abstract Classes and When Interfaces.

If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
Diversities between an abstract method & virtual method ?

An Abstract method does not provide an implementation and forces overriding to the deriving class (unless the deriving class also an abstract class), where as the virtual method has an implementation and leaves an option to override it in the deriving class. Thus Virtual method has an implementation & provides the derived class with the option of overriding it. Abstract method does not provide an implementation & forces the derived class to override the method.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories