protected Members

Chapter 9 discussed access modifiers public, private, and internal. A class's public members are accessible wherever the application has a reference to an object of that class or one of its derived classes. A class's private members are accessible only within the class itself. A base class's private members are inherited by its derived classes, but are not directly accessible by derived class methods and properties. In this section, we introduce access modifier protected. Using protected access offers an intermediate level of access between public and private. A base class's protected members can be accessed by members of that base class and by members of its derived classes. (Members of a class can also be declared protected internal. A base class's protected internal members can be accessed by members of that base class, by members of its derived classes and by any class in the same assembly.)

All non-private base class members retain their original access modifier when they become members of the derived class (i.e., public members of the base class become public members of the derived class, and protected members of the base class become protected members of the derived class).

Derived class methods can refer to public and protected members inherited from the base class simply by using the member names. When a derived class method overrides a base class method, the base class version of the method can be accessed from the derived class by preceding the base class method name with the keyword base and the dot (.) operator. We discuss accessing overridden members of the base class in Section 10.4.

Software Engineering Observation 10 1

Methods of a derived class cannot directly access private members of the base class. A derived class can change the state of private base class fields only through non-private methods and properties provided in the base class.

Software Engineering Observation 10 2

Declaring private fields in a base class helps you test, debug and correctly modify systems. If a derived class could access its base class's private fields, classes that inherit from that base class could access the fields as well. This would propagate access to what should be private fields, and the benefits of information hiding would be lost.


Relationship between Base Classes and Derived Classes

Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Flylib.com © 2008-2020.
    If you may any questions please contact us: flylib@qtcs.net