Sunday, June 26, 2011

.Net Interview Questions - OOP

What is Object Oriented Programming (OOP)?
Object-oriented programming (OOP) is a programming language model organized around objects. At this poing you may have a question in your mind, what is an object? its simple, an object can be anything which has some attributes and hehaviour. For example, hope now you are infront of a computer monitor, the monitor itself is an object, it has a lot of attributes like Make, Size, Type etc...

In OOP we use classes and objects to represent our data and logic. For example consider the following C# example.

public class Monitor
{
public string Make
{
set;
get;
}

public int Height
{
set;
get;
}

//etc......

}
The above class defines a structure of the computer monitor. An object of this class can represent a computer monitor.
OOP Concepts


  1. Data Abstraction : The act of representing essential features without showing the back ground details.

  2. Data Encapsulation: The wrapping up of data together into a single unit.

  3. Inheritance: The act of inheriting properties of an object into another.

  4. Polymorphism: The ability of a data to behave in multiple forms.

Possible questions about OOP concepts from a .NET stand point are:



  1. What is Data Abstraction? ans: see above definition

  2. What is Encapsulation? ans: see above definition

  3. What is inheritance? ans: see above definition

  4. What is Polymorphism? ans: see above definition

  5. Does .Net support multiple inheritance? ans: no

  6. Example for polymorphism? ans:function overloading, overriding and operator overloading

  7. Real world example for data abstraction? ans: when we press the start switch of a car or bike will get started, we might not need to know the internal process. we gave an input and we got an outcome.

  8. What is a class and an object? ans: class is a blueprint for creating objects. An object is an instance of a class and it have the properties and behaviours defined in the class.

Namespace in C# and its Advantages

Namespaces are a way of grouping type names and reducing the chance of name collisions. A namespace can contain both other namespaces and types. The namespace keyword is used to declare a scope.

We can organize our types based on their bahaviour and functionalities. For example consider the following example. Suppose you have a collection of songs, how will you store it in your computer? normally one do this by creating root folder named "Songs" then create sub folders based on song types, then create subfolders for albums etc....

Have a look on the following C# example to understand the advantages of the namespace.

namespace Songs
{
public class Songs
{
public void PlayAll()
{
Console.WriteLine("Playing all songs..");
}
}
namespace English
{
public class Titanic
{
public void Play()
{
Console.WriteLine("Playing titanic..");
}
}
}
namespace Malayalam
{
public class Urumi
{
public void Play()
{
Console.WriteLine("Playing urumi.....");
}
}
}
}