What is Encapsulation? In Object Oriented programming - TopicsExpress



          

What is Encapsulation? In Object Oriented programming Encapsulation is the first pace. The wrapping of data and function together in a single unit is called encapsulation. It tells how to bind data. This is useful to hide the data which is the new feature added to oops for providing security. • Encapsulation is one of the fundamental principles of object-oriented programming. • Encapsulation is a process of hiding all the internal details of an object from the outside world • Encapsulation is the ability to hide its data and methods from outside the world and only expose data and methods that are required • Encapsulation is a protective barrier that prevents the code and data being randomly accessed by other code or by outside the class • Encapsulation gives us maintainability, flexibility and extensibility to our code. • Encapsulation makes implementation inaccessible to other parts of the program and protect from whatever actions might be taken outside the function or class. • Encapsulation provides a way to protect data from accidental corruption • Encapsulation hides information within an object • Encapsulation is the technique or process of making the fields in a class private and providing access to the fields using public methods • Encapsulation gives you the ability to validate the values before the object user change or obtain the value • Encapsulation allows us to create a "black box" and protects an objects internal state from corruption by its clients NEED FOR ENCAPSULATION: The need of encapsulation is to protect or prevent the code (data) from accidental corruption due to the silly little errors that we are all prone to make. In Object oriented programming data is treated as a critical element in the program development and data is packed closely to the functions that operate on it and protects it from accidental modification from outside functions. Encapsulation provides a way to protect data from accidental corruption. Rather than defining the data in the form of public, we can declare those fields as private. The Private data are manipulated indirectly by two ways. ENCAPSULATION USING ACCESSORS AND MUTATORS: Let us see an example of Department class. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Encapsulation { public class Department { private string departname; //.......// Accessor. public string GetDepartname() { return departname; } // Mutator. public void SetDepartname( string a) { departname=a; } static int Main(string[] args) { Department d = new Department(); d.SetDepartname("ELECTRONICS"); Console.WriteLine("The Department is :"+d.GetDepartname()); return 0; } } } O/P:The Department is ELECTRONICS.
Posted on: Wed, 10 Jul 2013 07:07:48 +0000

Trending Topics



Recently Viewed Topics




© 2015