C#
  Home arrow C# arrow Page 4 - Defining Member and Type Visibility
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Defining Member and Type Visibility
By: Ayad Boudiab
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-04-16

    Table of Contents:
  • Defining Member and Type Visibility
  • Public Members
  • Private Members
  • Protected Members
  • Internal and Protected Internal Members
  • Type Visibility

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Defining Member and Type Visibility - Protected Members


    (Page 4 of 6 )

    Another access modifier supported in C# is protected. A protected member is accessible within its class and by derived classes. Derived classes are classes that inherit the functionality of classes higher in the hierarchy. For example, a son inherits some of the characteristics of his father, a circle inherits some of the characteristics of a shape, and so on…

    In the case of managers and employees, we know that a manager is, after all, an employee. So it makes sense to create data members inside the Employee class that only the Manager class can access (or any other class that inherits from Employee (like SalesPerson)). Here is an illustration:


    class Program

    {

    static void Main(string[] args)

    {

    Manager m = new Manager("Tim", 12345, 1500);

    m.PrintInfo();

    }

    }


    class Employee

    {

    protected string name;

    protected int id;


    public Employee(string e_name, int e_id)

    {

    name = e_name;

    id = e_id;

    }

    }


    class Manager : Employee

    {

    private double bonus;


    public Manager(string m_name, int m_id, double m_bonus)

    : base(m_name, m_id)

    {

    bonus = m_bonus;

    }


    public void PrintInfo()

    {

    Console.WriteLine("Manager " + name +

    " with id " + id + " has bonus " + bonus);

    }

    }


    Notice that name and id in the Employee class were successfully accessed from the Manager class because they were declared protected in the Employee class. And Manager inherits from Employee (C# uses the semicolon (:) to illustrate inheritance). In the Manager constructor, we used the base keyword to access the Employee’s constructor in order to initialize name and id.

    More C# Articles
    More By Ayad Boudiab


       · Hello everyone,Welcome to my first article of the series of introduction to C# and...
     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT