8.1 Introduction to Abstraction
Abstraction is an important feature of C# language. Abstraction means showing only the necessary details in the application. The information is present, but only relevant information is provided to the user. The programs written by the user is the abstracted data. Abstraction is the fundamental way for managing the complexity.
Advantages of Abstraction:
1) It helps user to manage the complexity of large system
2) It supports the reusability and modifiability of data
3) It supports the creation of generic modules that can be used by other system
4) The code is simplified due to the concept of abstraction
Consider an example of a car. Car has make,name, color, engine, mirror, brakes, steering, battery and many other internal components. The necessary things for the person driving the car is steering, battery, mirrors and brakes. Other all the components are not necessary for the user.
The person is interested in only the information useful for him about the car. The abstraction is used to hide the unnecessary details from the user. It helps to reduce the complexity of the vehicle for the user.
8.2 Introduction of Encapsulation
Encapsulation is the process of combining data and functions in a physical or logical package. It allows the developer to prevent access to nonessential details of an application. It is a mechanism that binds the data together with the code and is kept safe.
Abstraction and encapsulation are different, but they contain some common features. Abstraction enables you to make the relevant information visible. Encapsulation helps user to package the information to implement the specified level of abstraction. Hence, encapsulation assists abstraction by providing ways of hiding the non essential details from the user.
8.3 Implementing Encapsulation by using access specifiers
An access specifier is used for defining the scope of the class member. The variables and functions present in the class are represented as class members. A program contains one or more classes. User may need some members of the class to be accessed by other classes.
A programmer can use access specifiers to implement encapsulation and abstraction in C# programs.
The benefits of encapsulation in C# are as mentioned below:
1) The data in an application cannot be corrupted
2) The flexibility and extensibility of the code and reduction of the complexity
3) It lowers the coupling between the objects and hence improves the code maintainability
There are various type specifiers to specify the extent of the visibility of the class member. The types and functionality is as mentioned below:
The following access specifiers are supported by C#:
1) public
2) private
3) protected
4) internal
5) protected internal
1) The public access specifier
The public access specifier helps a class to share the member variables and member functions with other classes. Member declared as public can be accessed from outside the class.
The following code snippet shows the use of the public access specifier:
class Program { class Bike { public string BikeColor; } static void Main ( string[ ] args ) { Program p =new Program(); Bike Honda = new Bike(); Honda.BikeColor = “Red”; Console.WriteLine(“The color of the bike is:”+Honda.BikeColor); Console.Read(); } }
The output for the code is as shown below:
2) The Private access specifier
The private access specifier helps the class to hide its member variables and member functions from other class objects and functions. Hence, the private members of a class are invisible outside the class. If a member is declared as private, only the functions of the class can access the members. The instance of the class is not permitted to access its private members.
The following code shows the private access specifiers is as shown below:
class Car { private string Model; void Horn() { Console.WriteLine(“Beep Beep”); } public void SetModel() { Console.WriteLine(“Enter the model name”); Model = Console.ReadLine(); } public void Display() { Console.WriteLine(“The Horn is”); } class Show { static void Main ( string[ ] args ) { Car f = new Car(); f.SetModel(); f.Display(); f.Horn(); Console.ReadLine(); } } }
The output for the code is as shown below:
3) The Protected Access Specifier
The protected access specifier helps a class to hide the member variables and functions from other class objects and functions. Only the child class can access them. The protected access specifier is important when the user needs to implement inheritance.
The code sample to demonstrate the protected access specifier is as shown below:
class access1 { protected string name; public void display() { Console.WriteLine(“Name is:”+name ); } class Program { static void Main ( string [ ] args ) { access1 a1 = new access1(); Console.WriteLine(“Enter the name\t”); a1.name = Console.ReadLine(); a1.display(); Console.ReadLine(); } } }
The output for the code is as shown below:
4) The internal access specifier
The internal access specifier provides the class to expose its member variables and functions to other class objects and functions. The members defined as internal are accessed from any class or method defined in the same application in which the member is defined. The internal is the default access specifier for the class.
The code snippet for the internal access specifier is as shown below:
class access1 { internal void Show() { Console.WriteLine(“The color is Blue”); } class Program { Internal string BackColor = “Red”; } class Result { static void Main ( string [ ] args ) { access1 a = new access1(); a.Show(); Console.ReadLine(); } } }
The output of the code is as shown below:
5) The protected internal access specifier
The protected internal access specifier helps the class to expose the member variables and functions to the containing class, child class, or classes in the same application. It allows the access to the derived class outside the application. The protected internal access specifier is important for the implementation of inheritance.
The code snippet for protected internal access specifier is as shown below:
class access1 { protected internal string model; public void SetModel() { Console.WriteLine(“Enter the model name”); model = Console.ReadLine(); } public void display() { Console.WriteLine(“The model is:”); } class Result { static void Main (string[ ] args ) { access1 a = new access1(); a.SetModel(); a.display(); Console.WriteLine(a.model); Console.ReadLine(); } } }
The output for the code is as shown below:
The following table shows the visibility of the class members for the access specifiers in C#.
Access Specifiers | Available to objects of other class inside the namespace | Available to objects of child classes within the namespace | Available to objects of other classes outside the namespace | Available to objects of child classes outside the namespace |
public | Yes | Yes | Yes | Yes |
private | No | No | No | No |
protected | No | Yes | No | Yes |
Internal | Yes | Yes | No | No |
protected internal | Yes | Yes | No | Yes |