what is encapsulation with example? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what is encapsulation with example?

22nd Sep 2016, 4:43 PM
venkata veerendra manikumar kancharla
venkata veerendra manikumar kancharla - avatar
3 Answers
+ 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.
22nd Sep 2016, 4:45 PM
Adulkhaliq Q Hamad
Adulkhaliq Q Hamad - avatar
+ 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(); } } }
22nd Sep 2016, 4:48 PM
Adulkhaliq Q Hamad
Adulkhaliq Q Hamad - avatar
+ 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
27th Sep 2016, 5:13 AM
Vishwanath Patil
Vishwanath Patil - avatar