Skip to main content

OOPs Concepts: When to use

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.
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.
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.

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:

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
{
 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
{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.
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.

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.
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&

Comments

Popular posts from this blog

Death of a Star! in Bollywood

When I was flipping through my Diary 2002 the other day, my jottings about Monal’s tragic end beckoned me and brought to my mind a host of awesome nostalgic thoughts. I had a feeling of déjà vu as the dead artist again touched a chord in my heart. How would emotionally deplete artists behave when driven to wall? [Read on…] Monal, a starlet from Bollywood, hung herself to death on the Tamil New Year’s Day. A strange quirk of fate played havoc with her life, putting off her promising career of becoming an icon in the Kollywood. TV visuals showed her sleeping eternally on a bier; she was a feast to flies that were swarming her lissome body … a body that set fire to the hearts of thousands of her fans. Monal’s premature death moved me to a great extent not because she was one of the upcoming actors of the Kollywood and a diva for whom the tinsel world plumped rather madly, but because it set me thinking as to what led the young actor to kill herself savagely in the middle of her

11-awesome-diwali-lighting-decoration-ideas

Beautiful Flower Lights On Water Diwali Decoration Ideas Picture Beautiful Hanging Lighting Lanterns Diwali Decoration Beautiful Lighting Decoration For Diwali Beautiful Lighting Lamps Decoration Cool Diwali Lighting Decoration Ideas Diwali Lighting Decoration Picture Diwali Lighting Decoration Ideas Elegant Hanging Lighting Lamp Decoration Glass Bottles Hanging Lighting Lamps Diwali Decoration Hanging Beautiful Lighting Kandil Diwali Decoration Photo Shubh Labh Lighting Decoration For Diwali

Ssc combined graduate level exam syllabus and exam detail

q Tier-I of the Examination: General Intelligence & Reasoning : It would include questions of both verbal and non-verbal type. This component may include questions on analogies, similarities and differences, space visualization, spatial orientation, problem solving, analysis, judgment, decision making, visual memory, discrimination, observation, relationship concepts, arithmetical reasoning and figural classification, arithmetic number series, non-verbal series, coding and decoding, statement conclusion, syllogistic reasoning etc. The topics are, Semantic Analogy, Symbolic/Number Analogy, Figural Analogy, Semantic Classification, Symbolic/Number Classification, Figural Classification, Semantic Series, Number Series, Figural Series, Problem Solving, Word Building, Coding & de-coding, Numerical Operations, symbolic Operations, Trends, Space Orientation, Space Visualization, Venn Diagrams, Drawing inferences, Punched hole/pattern –folding & unfolding, Figural Pattern – folding