C# Tutorial
Part 27:Encapsulation
www.siri-kt.blogspot.com
• Encapsulation is defined 'as the process of enclosing one or
more items within a physical or logical package'.
• Encapsulation, in object oriented programming methodology,
prevents access to implementation details.
• Encapsulation: capsulate the data through instance variable.
hiding the data into single unit. by default every class is
private access specifier
• Abstraction and encapsulation are related features in object
oriented programming. Abstraction allows making relevant
information visible and encapsulation enables a programmer
to implement the desired level of abstraction.
• Capsulate the data through instance variable .binding the
data into a single unit. the data encapsulation is the concept
of hiding the data into a single unit.
• By default class area is private .private data we can using
public member functions only in same class. so private data
we cannot access from outside class.
• class student
• {
• int m1, m2, m3, tms;
• public void tmarks()
• { m1 = 80;
• m2 = 90;
• m3 = 90;
• tms = m1 + m2 + m3;
• Console.WriteLine(tms.ToString());
• Console.ReadLine(); }
• static void Main(string[] args)
• { student obj = new student();
• obj.tmarks(); }
• Access specifier(modifier):
• It specifies the scope of the variable.ie one variable features we can
access to different child classes.
• it is classified into 5 types
• 1.public-with class, child class & main function
• 2.private-with in the class
• 3.protected-with in the base class & child class
• 4.internal-with in the class libray (.dll)
• 5.protected internal-with in the class library (.dll)&child class
• Public:
• Public access specifier allows a class to expose its member variables and
member functions to other functions and objects. Any public member can
be accessed from outside the class.
• these variable features we can access with in the class,child class, main
function.
• Private:
• Private access specifier allows a class to hide its member variables and
member functions from other functions and objects.
• Only functions of the same class can access its private members. Even an
instance of a class cannot access its private members.
• These variable features we can access with in the class .
• Protected:
• These variable features we can access with in the class & child
class.
• Protected access specifier allows a child class to access the
member variables and member functions of its base class.
• This way it helps in implementing inheritance. We will discuss this in
more details in the inheritance chapter.
• Internal:
• These variable features we can access with in the class libraries.
• Internal access specifier allows a class to expose its member variables and member
functions to other functions and objects in the current assembly. In other words, any
member with internal access specifier can be accessed from any class or method
defined within the application in which the member is defined.
• Protected:
• The protected internal access specifier allows a class to hide its member variables and
member functions from other class objects and functions, except a child class within
the same application. This is also used while implementing inheritance.
• these variable features we can accesss within the class library &child class
• class student//Base class
• { protected int m1;//protected varaible
• public void emarks()//method
• { m1 = 80;
• Console.WriteLine(m1.ToString());
• Console.ReadLine();
• } }
• class dept:student//child class
• { int m2, m3, tms;//variable
• public void tmarks()//child class method
• { m2 = 80;
• m3 = 90;
• tms = m1 + m2 + m3;
• Console.WriteLine(tms.ToString());
• Console.ReadLine();
• }
• static void Main(string[] args)
• { dept obj = new dept();//instance varaible
• obj.emarks();//calling the base class method
• obj.tmarks();//calling the child class method
• }}}
For more visit our website www.siri-kt.blogspot.com
Thanks for
Watching
More Angular JS TutorialsMore C sharp (c#) tutorials

27c

  • 1.
  • 2.
    • Encapsulation isdefined 'as the process of enclosing one or more items within a physical or logical package'. • Encapsulation, in object oriented programming methodology, prevents access to implementation details. • Encapsulation: capsulate the data through instance variable. hiding the data into single unit. by default every class is private access specifier • Abstraction and encapsulation are related features in object oriented programming. Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired level of abstraction. • Capsulate the data through instance variable .binding the data into a single unit. the data encapsulation is the concept of hiding the data into a single unit. • By default class area is private .private data we can using public member functions only in same class. so private data we cannot access from outside class.
  • 3.
    • class student •{ • int m1, m2, m3, tms; • public void tmarks() • { m1 = 80; • m2 = 90; • m3 = 90; • tms = m1 + m2 + m3; • Console.WriteLine(tms.ToString()); • Console.ReadLine(); } • static void Main(string[] args) • { student obj = new student(); • obj.tmarks(); }
  • 4.
    • Access specifier(modifier): •It specifies the scope of the variable.ie one variable features we can access to different child classes. • it is classified into 5 types • 1.public-with class, child class & main function • 2.private-with in the class • 3.protected-with in the base class & child class • 4.internal-with in the class libray (.dll) • 5.protected internal-with in the class library (.dll)&child class • Public: • Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class. • these variable features we can access with in the class,child class, main function.
  • 5.
    • Private: • Privateaccess specifier allows a class to hide its member variables and member functions from other functions and objects. • Only functions of the same class can access its private members. Even an instance of a class cannot access its private members. • These variable features we can access with in the class . • Protected: • These variable features we can access with in the class & child class. • Protected access specifier allows a child class to access the member variables and member functions of its base class. • This way it helps in implementing inheritance. We will discuss this in more details in the inheritance chapter.
  • 6.
    • Internal: • Thesevariable features we can access with in the class libraries. • Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly. In other words, any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined. • Protected: • The protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application. This is also used while implementing inheritance. • these variable features we can accesss within the class library &child class • class student//Base class • { protected int m1;//protected varaible • public void emarks()//method • { m1 = 80; • Console.WriteLine(m1.ToString()); • Console.ReadLine(); • } }
  • 7.
    • class dept:student//childclass • { int m2, m3, tms;//variable • public void tmarks()//child class method • { m2 = 80; • m3 = 90; • tms = m1 + m2 + m3; • Console.WriteLine(tms.ToString()); • Console.ReadLine(); • } • static void Main(string[] args) • { dept obj = new dept();//instance varaible • obj.emarks();//calling the base class method • obj.tmarks();//calling the child class method • }}}
  • 8.
    For more visitour website www.siri-kt.blogspot.com Thanks for Watching More Angular JS TutorialsMore C sharp (c#) tutorials