What is Abstraction?
Abstraction is a process of hiding the implementation
details and displaying the essential features.
Example1: A
Laptop consists of many things such as processor, motherboard, RAM, keyboard,
LCD screen, wireless antenna, web camera, usb ports, battery, speakers
etc. To use it, you don't need to know how internally LCD screens, keyboard,
web camera, battery, wireless antenna, speaker’s works. You just
need to know how to operate the laptop by switching it on. Think about if you
would have to call to the engineer who knows all internal details of the
laptop before operating it. This would have highly expensive as well as not
easy to use everywhere by everyone.
So here the Laptop is an object that is designed
to hide its complexity.
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:-
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;
}
In object-oriented software, complexity is managed by
using abstraction.
Comments
Post a Comment