Resources » .NET programming » .NET Framework

OOPS Concepts in .Net to Shake your fundamentals– Part 1


Posted Date: 13-Jun-2008  Last Updated:   Category: .NET Framework    
Author: Member Level: Gold    Points: 10



Introduction


OOPS or Object Oriented Programming Concepts but some of the concepts have been always used in one or the other programming languages. For example you must have used structs in C which is a good example of encapsulation. There are four major pillar of OOPS. Let’s try to understand each one of them by taking some examples also:-
1.) Encapsulation
2.) Abstraction
3.) Polymorphism
4.) Inheritance

Encapsulation:-


Interview Definition:- Binding data and member functions together inside a single unit.
How to Encapsulate:- By creating types e.g Classes and Struct
Bu using encapsulation we can create out own custom types by reusing the existing or primitive types.

Abstraction:-


Abstraction defines way to abstract or hide your data and members from outside world. Simply speaking Abstraction is hiding the complexities of your class or struct or in a generic term Type from outer world. This is achieved by means of access specifiers.
Interview Definition: - Hiding the complexities of your type from outside world.
How to Abstract: - By using Access Specifiers
.Net has five access specifiers:-
Public -- Accessible outside the class through object reference.
Private -- Accessible inside the class only through member functions.
Protected -- Just like private but Accessible in derived classes also through member functions.
Internal -- Visible inside the assembly. Accessible through objects.
Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member functions.
Let’s try to understand by a practical example:-
Interview Tip:-
The default access specifier for a class in internal. Mind it I mean class not class’s data members.


public class Class1
{
int i; //No Access specifier means private
public int j; // Public
protected int k; //Protected data
internal int m; // Internal means visible inside assembly
protected internal int n; //inside assembly as well as to derived classes outside assembly
static int x; // This is also private
public static int y; //Static means shared across objects
[DllImport("MyDll.dll")]
public static extern int MyFoo(); //extern means declared in this assembly defined in some other assembly
public void myFoo2()
{
//Within a class if you create an object of same class then you can access all data members through object reference even private data too
Class1 obj = new Class1();
obj.i =10; //Error can’t access private data through object.But here it is accessible.:)
obj.j =10;
obj.k=10;
obj.m=10;
obj.n=10;
// obj.s =10; //Errror Static data can be accessed by class names only
Class1.x = 10;
// obj.y = 10; //Errror Static data can be accessed by class names only
Class1.y = 10;
}
}

Now lets try to copy the same code inside Main method and try to compile
[STAThread]
static void Main()
{
//Access specifiers comes into picture only when you create object of class outside the class
Class1 obj = new Class1();
// obj.i =10; //Error can’t access private data through object.
obj.j =10;
// obj.k=10; //Error can’t access protected data through object.
obj.m=10;
obj.n=10;
// obj.s =10; //Errror Static data can be accessed by class names only
Class1.x = 10; //Error can’t access private data outside class
// obj.y = 10; //Errror Static data can be accessed by class names only
Class1.y = 10;
}
What if Main is inside another assembly
[STAThread]
static void Main()
{
//Access specifiers comes into picture only when you create object of class outside the class
Class1 obj = new Class1();
// obj.i =10; //Error can’t access private data through object.
obj.j =10;
// obj.k=10; //Error can’t access protected data through object.
// obj.m=10; // Error can’t access internal data outside assembly
// obj.n=10; // Error can’t access internal data outside assembly

// obj.s =10; //Errror Static data can be accessed by class names only
Class1.x = 10; //Error can’t access private data outside class
// obj.y = 10; //Errror Static data can be accessed by class names only
Class1.y = 10;
}

There are two more scenerios for internal data members that we will discuss after inheritance also protected data member will be more clear once we have some idea about inheritance model.In the next article we will be discussing Polymorphism and Inheritance in depth.


Did you like this resource? Share it with your friends and show your love!

Responses to "OOPS Concepts in .Net to Shake your fundamentals– Part 1"
Author: Rakesh Kumar    14 Jun 2008Member Level: Bronze   Points : 0
Nice Article


Author: Bindu Bujji    19 Jun 2008Member Level: Gold   Points : 0
Thanks for sharing with us.

Bindu



Author: Ramkumar    20 Jun 2008Member Level: Bronze   Points : 0
thanks


Author: T.C.P.Elavarasu    23 Jun 2008Member Level: Silver   Points : 0
Good article


Author: Roopesh Babu Valluru    25 Jun 2008Member Level: Gold   Points : 0
thx for ur post...


Author: Abhijeet    25 Jun 2008Member Level: Bronze   Points : 0
this is very good to prepare our basics



Author: aNiL    25 Jun 2008Member Level: Silver   Points : 1
good article. nice things

waiting for next article



Author: Diva    04 Aug 2008Member Level: Bronze   Points : 0
Very helpful


Author: navaneethkn    13 Aug 2008Member Level: Silver   Points : 1
"protected internal int n; //inside assembly as well as to derived classes outside assembly"

The above one said by you is wrong. "protected internal" will be accessible only to the class where it is declared and to all the derived classes in the same assembly, not outside assembly.



Author: Abhishek Iyer    01 Oct 2010Member Level: Bronze   Points : 0
Nice article


Author: Modi Ashfaq Ahmed    08 Dec 2010Member Level: Silver   Points : 1
nice article,
thanks for sharing,
keep the good work going.

regards
Ashfaq Modi.



Author: Jayendra Kumar    04 Jan 2011Member Level: Gold   Points : 0
Thanks for such a good article.


Guest Author: Ankesh     18 Jun 2012
This is very good ooops concept sir ji


Guest Author: rschandras     13 Feb 2013
Abstraction doesn't mean "hide your data and members from outside world". This is exactly what you mean by encapsulation.
Abstraction is representing the real world object in terms of a class. This class should have all the essential properties/methods of your concern.

e.g. when you want to represent a savings bank customer, you may need all his personal information like name, address, business/salary, etc. You may not need details of information about customer's educational qualifications, probably his/her last degree will be sufficient. But in case of an HR application, you will require full educational background.



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India