Illustrates the use of various access modifiers : Class Access Modifiers « Language Basics « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Language Basics » Class Access ModifiersScreenshots 
Illustrates the use of various access modifiers
Illustrates the use of various access modifiers

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example5_10.cs illustrates the use of various
  access modifiers
*/


// declare the Car class
class Car
{

  // declare the fields
  public             string make;
  protected internal string model;
  internal           string color;
  protected          int horsepower = 150;
  private            int yearBuilt;

  // define the methods
  public void SetYearBuilt(int yearBuilt)
  {
    this.yearBuilt = yearBuilt;
  }

  public int GetYearBuilt()
  {
    return yearBuilt;
  }

  public void Start()
  {
    System.Console.WriteLine("Starting car ...");
    TurnStarterMotor();
    System.Console.WriteLine("Car started");
  }

  private void TurnStarterMotor()
  {
    System.Console.WriteLine("Turning starter motor ...");
  }

}


public class Example5_10
{

  public static void Main()
  {

    // create a Car object
    Car myCar = new Car();

    // assign values to the Car object fields
    myCar.make = "Toyota";
    myCar.model = "MR2";
    myCar.color = "black";
    // myCar.horsepower = 200;  // protected field not accessible
    // myCar.yearBuilt = 1995;  // private field not accessible

    // call the SetYearBuilt() method to set the private yearBuilt field
    myCar.SetYearBuilt(1995);

    // display the values for the Car object fields
    System.Console.WriteLine("myCar.make = " + myCar.make);
    System.Console.WriteLine("myCar.model = " + myCar.model);
    System.Console.WriteLine("myCar.color = " + myCar.color);

    // call the GetYearBuilt() method to get the private yearBuilt field
    System.Console.WriteLine("myCar.GetYearBuilt() = " + myCar.GetYearBuilt());

    // call the Start() method
    myCar.Start();
    // myCar.TurnStarterMotor();  // private method not accessible

  }

}

           
       
Related examples in the same category
1. Demonstrate protectedDemonstrate protected
2. Public vs private accessPublic vs private access
3. illustrates member accessibilityillustrates member accessibility
4. illustrates member hidingillustrates member hiding
w_w___w__.___j__a___v___a_2_s.___c___o_m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.