What is OOPS?
OOPS stands for Object Oriented Programming System.Application developed using OOPS concepts consists of a collection of objects.
Objects interact with each other to achieve a desired functionality.
Class is a template which defines the structure of an object.Once you define a class ,you define the attributes and behavior that the objects of that class will have.Class is a abstract concept.Objects actually exist in the memory.
What are basic concepts of OOPS?
Main OOPS concepts are:
- Abstraction Class defines only the relevant details about the actual entity
- Encapsulation Private data of the object is hidden from outside world
- Inheritance A new class can reuse the behavior and attributes of an existing class by using inheritance
- Polymorphism Polymorphism means same interface but different implementations.The client invokes the same interface to call different functionalities.Example of polymorphism is method overloading.
What are constructors and destructors?
Cosntructor is a method of the class which has the same name as the class.So if the class name is A then constructor of that class will be called A.Constructor is used to initialize the members of the class and is called when an object of a class is created.Unlike other methods constructors are not inherited by the derived class.
Destructor of the class is a method that is called when an object of the class is destroyed.If the object uses some resources such as database connections or file handles then those should be closed in the destructor.
What is method overloading and method overriding?
Method overloading means multiple methods are defined in a class having the same name.The overloaded methods have the same name but different method arguments.Number
of Method arguments and their types forms the method signature.So overloaded methods have different signatures.
Following are the two overloads of the Add method.First method takes int arguments while the second method takes float arguments.
public int Add(int a,in b)
public float Add(float a,float b)
Unlike overloading in method oveririding base class and derived classes are involved.A method declared in the base class can be replaced in the derived class with a different implementation.The implementation in the derived class is said to override the method in the base class.The method in the derived class has the same number
and type of parameters as the method in the base class.
In the following example class A defines a method called SendMail.The SendMail method is overriden in the derived class B.The method that will be called depends on
the type of object.If the type of object is A then the SendMail method in class A will be called ,if the type of object is B then B's SendMail method will be called.
public virtual void SendMail()
public override void SendMail()
When will you use struct instead of class?
Struct is useful when you want to define a simple class with few properties and methods.When you don’t want encapsulation you choose struct.In C# you can not inherit a struct where as you can inherit a class.In C# struct is a value type whereas class is a reference type.
What are access modifiers?
Access modifiers are used to define the accessibility of class and class members.They are used to define from where the members of a class can be accessed from.Some of the common access modifiers are:
- public class or member is visible everywhere in your application
- internal visible only within same assembly
- protected visible only within the same or derived class
- protected internal visible only within same assembly or derived class
- private class member is restricted to access from only within class.