Public vs private access : Class Access Modifiers « Class Interface « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » Class Access ModifiersScreenshots 
Public vs private access
Public vs private access

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Public vs private access. 
 
using System; 
 
class MyClass {  
  private int alpha; // private access explicitly specified 
  int beta;          // private access by default 
  public int gamma;  // public access 
 
  /* Methods to access alpha and beta.  It is OK for a 
     member of a class to access a private member 
     of the same class. 
  */ 
  public void setAlpha(int a) { 
    alpha = a;  
  
 
  public int getAlpha() { 
    return alpha; 
  
 
  public void setBeta(int a) { 
    beta = a;  
  
 
  public int getBeta() { 
    return beta; 
  
}  
  
public class AccessDemo {  
  public static void Main() {  
    MyClass ob = new MyClass();  
  
    /* Access to alpha and beta is allowed only 
       through methods. */ 
    ob.setAlpha(-99)
    ob.setBeta(19)
    Console.WriteLine("ob.alpha is " + ob.getAlpha())
    Console.WriteLine("ob.beta is " + ob.getBeta())
 
    // You cannot access alpha or beta like this: 
//  ob.alpha = 10; // Wrong! alpha is private! 
//  ob.beta = 9;   // Wrong! beta is private! 
 
    // It is OK to directly access gamma because it is public. 
    ob.gamma = 99;  
   }  
}

           
       
Related examples in the same category
1.Using Access Modifiers
2.Member Accessibility
3.Member Hiding
4.Demonstrate protectedDemonstrate protected
5.illustrates member accessibilityillustrates member accessibility
6.illustrates member hidingillustrates member hiding
7.Illustrates the use of various access modifiersIllustrates the use of various access modifiers
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.