Illustrates the use of various access modifiers : 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 
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.Using Access Modifiers
2.Member Accessibility
3.Member Hiding
4.Demonstrate protectedDemonstrate protected
5.Public vs private accessPublic vs private access
6.illustrates member accessibilityillustrates member accessibility
7.illustrates member hidingillustrates member hiding
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.