0
what is encapsulation with example?
3 Réponses
+ 3
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.
+ 3
using System;
namespace RectangleApplication
{
class Rectangle
{
//member variables public double length; public double width;
public double GetArea() { return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea());
}
}
//end class Rectangle class ExecuteRectangle { static void Main(string[] args)
{
Rectangle r = new Rectangle(); r.length = 4.5; r.width = 3.5; r.Display();
Console.ReadLine();
}
}
}
+ 1
It is a process of hiding internal details of an object from out side world
e.g
class Base
{
public int id;
private string name;
}
Static Void Main(string [] args)
{
Base b=New Base();
b.id=1;
b.name="vishal" //error
}
in this e.g there is class Base with two objects id and name
id is public so it is accessible with in class and out side of class
but
name is encapsulated to private so it is only accessible with on class not for out side of class