C# Inheritance Member Access with Programming Example

access-levels-picture
In this chapter you will learn

  • Which type of member can be accessed by a child class?
  • Programming Examples and Codes

Which Type of Member can be accessed by a Child Class or Derived class?

A child class or derived class can access all the public, protected, internal and protected internal member. Private member cannot be accessed by child class however it is inherited and still present in child class and can be accessed using public property (GET SET modifier). There are two examples that demonstrate all the concept of member access clearly. First example will show which type of member can be accessed in child class and another example will show how to access private member in child class using GET SET modifier.




  • Programming Examples

    Output

    I am Public Method
    I am Protected Method
    I am protected internal method
    I am Internal Method
    _

     
     
    If you uncomment private_member() then you will get compile time error "'Member_Access.baseclass.private_member()' is inaccessible
    due to its protection level"

    Access Private Member in Child class

    This example shows how you can access private member in a child class.

    Programming Example

    Output

    Total : 26
    _

    Summary

    In this chapter you learned which type of base class member can be accessed in child class or derived class. You also learned to access base class private member in child class. The next tutorial will teach you Abstract and Virtual Methods in Inheritance.


    Leave a Reply

    Your email address will not be published. Required fields are marked *