HI WELCOME TO KANSIRIS

Advantage & disadvantage of encapsulation?

Leave a Comment

It's data hiding or data abstraction. Private member variables of a class are encapsulated so that they can only be accessed by specific methods/properties. This will prevent data corruption.



Whereas abstraction focuses on reducing the complexity of how we view the real world, encapsulation concentrates on the design of our C# source code. It is a powerful mechanism for reducing complexity and protecting data of individual objects on the programming level.

Encapsulation is the process of combining data and the actions (methods) acting on the data into a single entity. In OOP and hence C#, such an entity is equivalent to an object.



Encapsulation is a mechanism for hiding instance variables and irrelevant methods of a class from other objects. Only methods, which are necessary to use an object of the class, are exposed.



Encapsulation can be implemented using the following methods in C#



1. Using properties

2. Using accessor Mutator methods


ENCAPSULATION USING PROPERTIES



C++ programmers try to keep member variables private. This data hiding promotes encapsulation and allows you to change your implementation of the class without breaking the interface your clients rely on.



To the user, a property looks like a member variable, but to the implementor of the class it looks like a method. Properties allow you total encapsulation and data hiding while giving your users easy access to the members.

You can provide a Student class with an Age property to allow users to get and set the student's age member.



public int Age

{

get

{

return age;

}

set

{

age = value;

}

}



The keyword value is implicitly available to the property. If you write



Venkatesh.Age = 17;



the compiler will pass in the value 17 as value.

You can create a read-only property for his PerformanceRank by implementing the Get and not the Set accessor.



public int PerformanceRank

{

get

{

return performanceRank;

}

}



You can get Venkatesh's age through the property and then you can use that property to set the age. You can access the PerformanceRank property to obtain the value, but not to set it



If you decide later to retrieve the student's age from a database, you need change only the accessor implementation; the client will not be affected.



ENCAPSULATION USING ACCESSOR AND MUTATOR METHODS:



Let us see an example of an Employee class. To manipulate the data in that class (String empname) we define an accessor (get method) and mutator (set method).



using system;

public class Employee

{

private string empname;

.......



// Accessor Method

public string GetEmpname()

{

return empname;

}



// Mutator Method

public void SetEmpname( string tempname)

{

empname=tempname;

}



}



Like the above way we can protect the private data from the outside world. Here we use two separate methods to assign/access and get/retrieve the required data.



public static int Main(string[] args)

{

Employee e= new Employee()

e.SetEmpname("VENKATESH");

Console.WriteLine("The Department is :"+e.GetEmpname());

return 0;

}

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.