Lecture - 5 Encapsulation
Lecture - 5 Encapsulation
Encapsulation
Encapsulation
• Process of enclosing one or more items within a physical or logical package
• Public
• Private
• Protected
• Internal
Public Access Specifier
• Allows a class to expose its member variables and member functions to
other functions and objects.
namespace RectangleApplication {
class Rectangle {
class ExecuteRectangle {
//member variables
static void Main(string[] args) {
public double length;
Rectangle r = new Rectangle();
public double width;
r.length = 4.5;
r.width = 3.5;
public double GetArea() {
r.Display();
return length * width;
Console.ReadLine();
}
}
public void Display() {
}
Console.WriteLine("Length: {0}", length);
}
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}//end class Rectangle
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.
• Arguments
Method Elements
• Method body
• This contains the set of instructions needed to complete the required
activity.
• Parameters
• What's given in the function declaration/definition.
• Arguments
• What's passed when calling the function.
Method Code
static void Main(string[] args) {
using System; /* local variable definition */
int a = 100;
namespace CalculatorApplication {
int b = 200;
class NumberManipulator {
int ret;
NumberManipulator n = new
// parameters NumberManipulator();
public int FindMax(int num1, int
num2) {
//calling the FindMax method
/* local variable declaration */
int result; // arguments
ret = n.FindMax(a, b);
if (num1 > num2)
Console.WriteLine("Max value is :
result = num1;
else
{0}", ret );
result = num2; Console.ReadLine();
return result; }
} }
}
Thanks for Your Time!!!