VisualBuilder
  Home > Csharp > Tutorials > Encapsulation - C# Tutorial
Tell a friend
Link to us
Total Members
      Members: 84606
     
Sitemap Forum Chat
Home
C# Tutorial Home
1 . Introduction to C sharp
2 . Control Statement: Selection Statement in C sharp
3 . Control Statements – Loops Statement
4 . Methods
5 . Namespaces
6 . Introduction to Classes:
7 . Inheritance:
8 . Polymorphism
9 . Properties
10 . Indexers
11 . Structs
12 . Interfaces
13 . Delegates:
14 . Exception Handling:
15 . Attributes
16 . Enums
17 . Encapsulation
18 . Parameter Passing in C sharp
19 . Method Overloading
20 . Database Interaction Using C sharp
21 . Operator Overloading in C sharp -1
22 . Operator Overloading in C sharp -2
23 . Operator Overloading in C sharp -2
24 . Sockets
25 . DNS [Domain Name System]
26 . Working with Files
27 . Generating Help File in C sharp
28 . Code Access Security
29 . Multi-Threading
30 . Globalization and Localization -1
31 . Working with Registry in C sharp
32 . Globalization and Localization -2
33 . Windows Service
34 . Web Service
35 . Consuming Web Services
36 . Creating Proxy Object of Web Service
37 . Creating an XML document
38 . Reading XML document in C sharp
39 . Using XMLWriter class to Write XML document in C sharp
40 . Assembly Information : Getting Permission set of the assembly
41 . Creating your own Permission Set
42 . Using C sharp Socket
 
Csharp Group Home
Csharp Discussion (7)
Csharp Members (2250)
Csharp Resources
Csharp Source Code (35)
Csharp Articles (0)
Csharp Blogs
Csharp Jobs
Csharp Components (5)
Csharp Books
Csharp Websites (25)
Csharp News (17)
Csharp Q & A (4)
- Csharp Ask Question
- Csharp Questions
- Csharp Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
LINQ Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Javascript Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Grails Tutorial
Hibernate Tutorial
Java 1.6 Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Service-Oriented Architecture (SOA) Tutorial For Managers
Spring Tutorial
Struts Tutorial
Tomcat Tutorial

RESOURCES
Q & A (432 )
Source Code (3217 )
Articles (11 )
Components (1589 )
News (880 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise


Csharp Tutorial
 Encapsulation
  << Prev: Enums Next: Parameter Passing in C sharp >>

Encapsulation is the process of wrapping up of data and members in a class. Encapsulation provides the way to hide data from the external usage. This is important in the scenario when we don't want our precious data to be used or manipulated by external user. Encapsulation is an object-oriented principle of hiding the internal state and behavior of an object, making your code more maintainable.


 


Advantages of Encapsulation



  • Data-hiding through the access specifier private.

  • Reduce coupling between objects.

  • Increases the maintainability of code.

  • Wrapping up of data variables and members inside the class.


 


The following table will give the description of the Access modifiers available in C#.


 






























Access Modifier



Description (who can access)



Private


 



Private members can be accessed within the class where they are declared. We can't access private members outside the class, not even with the object of the class.


This basically provides the concept of Data-Hiding.



Protected



Protected members scope can be outside the class but only limited to the immediate derived class that is inheriting the base class where the protected members resides.


The protected can be used by the members of same class or the members of derived class.



Internal



Here the internal members are accessible within the same assembly.


If you don't specify any modifier, by default it is of internal type.


 



Protected Internal



Here either accessed in the same assembly or the derived assembly also. This is basically a combination of protected and internal access modifiers.



Public



Public members can be accessed anywhere. No restriction associated with this access modifier.



 


 


Sealed Class


The sealed keyword provides the restriction that no other class in the context can inherit this class. By this, we can make our class not to be inherited by other class. If the class tries to inherit the class that is declared as sealed, the compile time error occurs.


 


1] Class declared as sealed


sealed class ClsSealed17 {// Any members }


 


Abstract Class


The class declared with abstract keyword, this means the class is only provide the essential elements and not including the background details. Abstraction is the process of including the essential elements and hiding the details. In layman terms, we can say that a computer is the abstract form, because we are not aware of the internal technicality.


If the class is declared as abstract, we have to give the body to all the methods that are declared as abstract.


 


1] Class declared as abstract


 


abstract class clsAbstractDemo{


// This can contains abstract or concrete methods.


}


 


2] Abstract Members



public abstract string displayname();


Note:- The abstract methods can only include the signature of the method.


   


Example: Demonstrate Encapsulation


 




 


 


ClsEncapsulation17.cs


 


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace cSHARPEXAMPLES


{


class Customer


{


  public void CustomerRecord()


    {


      AddNewRecord();


      UpdateExistingRecord();


      DeleteRecord();


    }


  protected virtual string AddNewRecord()


    { return "Base: AddNewRecord" ; } //ADD NEW RECORD


  protected virtual string UpdateExistingRecord()


    { return "Base: UpdateExistingRecord" ; } //UPDATE EXISTING RECORD


  protected virtual string DeleteRecord()


    { return "Base: DeleteRecord" ; } //DELETE EXISTING RECORD


}


class ClsEncapsulation17 : Customer


{


  private string name = "" ; //Can't Access Outside the Class


  private string address = "" ; //Can't Access Outside the Class


  private string pri_Display() //Can't Access Outside the Class


    {


    return "This method can't be access outside the class" ;


    }


 


  public string CustomerName { //Public Property


      get { return name; }


      set { name = value ; }


    }


  public string CustomerAddress //Public property


    {


      get { return address; }


      set { address = value ; }


    }


  public string CombineDisplay() //Public Method


    {


      return CustomerName + " :::" + CustomerAddress;


    }


  protected override string AddNewRecord() //Protected Method


    {


      return "protected method :: AddNewRecord" ;


    }


  protected override string UpdateExistingRecord() //Protected Method


    {


      return "protected method :: UpdateExistingRecord" ;


    }


  protected override string DeleteRecord() //Protected Method


    {


        base .DeleteRecord();


    return "protected method :: DeleteRecord" ;


    }


  public string combineString()


    {


        string cmbStr = "" ;


        cmbStr = cmbStr + AddNewRecord() + "\n" ;


        cmbStr = cmbStr + UpdateExistingRecord() + "\n" ;


        cmbStr = cmbStr + DeleteRecord();


    return cmbStr;


    }


  }


}


 


ClsSealed17.cs


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace cSHARPEXAMPLES


{


  sealed class ClsSealed17


  {


  private string name = "" ;


  public string CustomerName


    {


    get { return name; }


    set { name = value ; }


    }


  public string displayName()


    {


    return name;


    }


  }


//Can't inherited a Sealed Class


  class derivedClass //: ClsSealed17


   { 


   }


 }


 


 


ClsAbstract17.cs


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace cSHARPEXAMPLES


{


 abstract class clsAbstractDemo


  {


      private int _number = 10;


   public string _name = "" ;


 


   public abstract string displayname(); // Abstract method


   public int calcTotal() //Concreate Method


    {


     return (_number * 10);


    }


}


  class ClsAbstract17 : clsAbstractDemo


    {


    public override string displayname() { //Abstract Method


    return "Abstract Method Provide Body in Derived Class" ;


    }


  }


}


 


Output


 


Click On: Private Members Button


 


Click On: Public Members Button




 


Click On: Protected Members Button




 


Click On: Sealed




 


 


Click On: Abstract


 


  << Prev: Enums Next: Parameter Passing in C sharp >>
Csharp Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
Skype vs. sipcall
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising