Hai Friends,
This post is regarding the main OOPs concepts and what is there
use and when it’s good to use them.
Below are the concepts which are included in this post:
·
Encapsulation
·
Abstraction
·
Overloading
·
Method Overriding
·
Constructor
·
Interface
·
Inheritance
·
Abstract class
These OOPs concepts you can use while implementing
the functionality of your projects in accordance with the
requirements and situations.
Hope it will be useful to learn and in the implementation of the
projects.
1) In which situation encapsulation is used in your project?
Encapsulation is hiding and binding of the data.
Accessing the public properties by using private variables is one of the good examples of Encapsulation in all the projects.
e.g.
Accessing the public properties by using private variables is one of the good examples of Encapsulation in all the projects.
e.g.
private string _name;
public String Name
{
get{return _name}
set{_name= value;}
}
Here you are not able to access the private variable _name but its value can be accessible by the public property Name. So a kind of Encapsulation here. Namespace is also an example of Encapsulation which is encapsulating the classes, methods, properties etc.
This is the runtime behaviour of an entity.
2) In which situation abstraction is used in your project?
Hiding the behaviour and showing the necessary things regards an entity can be called as Abstraction. Like for a Car, showing the method like Clutch, Gear etc but hiding its internal functions can be an example of Abstraction. This is the compile time behaviour of the entities.
When there are the specific requirements and that can be flown to their child, then we can use abstraction. it means whatever is essential, we implement to its child classes, else will be inherited automatically.
e.g.
When there are the specific requirements and that can be flown to their child, then we can use abstraction. it means whatever is essential, we implement to its child classes, else will be inherited automatically.
e.g.
class A
{
public abstract void
Add(int a, int b);
protected int x;
public string
Name{get; set;};
}
Class B: A
{
public override void Add(int a, int b)
{
Console.WriteLine("Sum of two numbers
is:" + int. Parse(a + b).toString());
}
}
Here the essential method Add is implemented to the child class B. rest all the members will be inherited accordance to their protection levels.
Here the essential method Add is implemented to the child class B. rest all the members will be inherited accordance to their protection levels.
3) In which situation method overloading is used in your
project?
The concept where we want the similar functionality but different behaviour, we use overloading.
When we have same functionality to implement with different behaviour, we go with the overloading of method.
e.g. If we have to get the sum of 2 numbers, 3 number and 4 numbers. So in that case we can take only one method name Add with its varied parameters like:
When we have same functionality to implement with different behaviour, we go with the overloading of method.
e.g. If we have to get the sum of 2 numbers, 3 number and 4 numbers. So in that case we can take only one method name Add with its varied parameters like:
public void Add(int a,
int b)
{
Console.WriteLine("Sum of 2 numbers"
+ int. Parse(a + b).toString());
}
public void Add(int a,
int b, int c)
{
Console.WriteLine("Sum of 3 numbers"
+ int. Parse(a + b +c).toString());
}
public void Add(int a,
int b, int c, int d)
{
Console.WriteLine("Sum of 4 numbers"
+ int. Parse(a + b + c + d).toString());
}
Now according to our requirement, we can call the respective
method with its arguments.
Here all the methods are doing sum of the numbers, but their behaviour are different- the first method will give the sum of 2 number while the next one will give the sum of 3 number and the final method will give the sum of 4 numbers.
4) In which situation method overriding is used in your project?
When we may(abstract) or may not(virtual) want to implement the particular method
at runtime to its child class according to the requirement, we go with
overriding concept.
e.g.
Class X
e.g.
Class X
{
public virtual void Print()
{
Console.WriteLine ("This is the print method.
You can override according to your definitions and implementation");
}
}
Class Y:X
{
public Override void Print()
{
Console.WriteLine("Implementation for
Excel print methods");
}
}
5) In which situation constructor is used in your project?
Constructor is the way to initialize the class with the default values of the members. Constructor is by default initialize with the class.
But if you want to initialize some members when the class in getting instantiate, you can write the constructor and code for initialization inside that constructor.
e.g.
Class MyClass
But if you want to initialize some members when the class in getting instantiate, you can write the constructor and code for initialization inside that constructor.
e.g.
Class MyClass
{ int a;
public MyClass()
{
a=10; //initialized with the class
instantiation
}
}
Here the constructor code will get initialized with the class instantiation.
6) In which situation interface is used in your project?
Interface is the collection of abstract members.
So whenever we have the requirements such as it requires through the application at many places, then we create an interface with that method so that we can implement it at our own way according to the requirements.
e.g.
So whenever we have the requirements such as it requires through the application at many places, then we create an interface with that method so that we can implement it at our own way according to the requirements.
e.g.
Interface Inf
{
void Print();
}
Class ExcelClass:Inf
{
public override void Print()
{
//Write code to print in excel
}
}
Class WordPrintClass:
Inf
{
public override void Print()
{
//Write code to print in Word
}
}
Class PDFPrintClass:
Inf
{
public override void Print()
{
//Write code to print in PDF
}
}
Also to implement the multiple inheritances, we use inheritance.
Also to implement the multiple inheritances, we use inheritance.
7) In which situation inheritance is used in your project?
When we want to have some common features for multiple classes
then we go for inheritance.
To inherit the properties of base class to their derived class.
To inherit the properties of base class to their derived class.
8) In which situation abstract class is used in your project?
Abstract class is same as abstract. So go to the Q 2 for the
details.
So you can go through the link to get the fair idea about its implementation:
http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=295763&
http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=295763&
thanku
ReplyDeleteaccordance with the requirements and situations. your topics is when to use and why to use. everbody know that project requirements. please post ur article which is your topics related
ReplyDeleteMany Thanks.
ReplyDeleteMany Thanks.
ReplyDeleteThank u very much :).. its help me lot
ReplyDeleteThanks :)
ReplyDeletethank you
ReplyDelete