CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C#: Classes – Access Specifiers & Inheritance

‘C#’ class members are accessible based on their access specifiers. I will write a separate article on class’s access specifiers in more detail. But here I want to give an high level introduction on access specifiers.

Access Specifiers

‘C#’ provides public, private, internal, protected and protected internal access specifiers. Remember that the class members are accessible within the same class irrespective of their access specifiers.

public classes or class members can be accessible from anywhere.

private classes or members are accessible only within the class only.

protected classes or members can be accessible within its derived classes.

internal classes or members can be accessible within the same assembly.

protected internal classes or members can be accessible within the derived classes and the derived classes must be in the same assembly.

Below is the syntax to specify an access specifier for a class and its members.

<access specifier> class ClassName
{
   <access specifier> Variable;
};

Inheritance

Another important feature, an Object Oriented Programming Language like ‘C#’ provides is Class Inheritance. A class (Derived class) can be inherited from another class (Base class).

Derived classes can access their base class members (if allowed); but base classes can’t access their derived class members.

One important thing to remember, ‘C#’ doesn’t support a multiple inheritance at class level where a derived class inherited from multiple base classes. But, multiple inheritance supports in interface level.

The syntax to create a derived class is:

<access specifier> class <Derived Class Name> : <Base Class Name>
{
   <Derived Class Members here>
};

Putting altogether here is the code:

// Sample.cs
namespace CodeSteps
{
    // Base class
    class Base
    {
        // Allowed to call within the "Base" class.
        private void PrivateDisplay()
        {
            System.Console.WriteLine("I am Base.PrivateDisplay!");
        }

        // Allowed to call anywhere.
        public void Display()
        {
            System.Console.WriteLine("I am Base!");
        }
    };

    // Derived class derived from Base; so it can access Base's allowed members.
    class Derived : Base
    {
        public void DerivedMember()
        {
            System.Console.WriteLine("I am Derived!");
        }

        static void Main()
        {
            Derived obj = new Derived();
            if (obj != null)
            {
                obj.DerivedMember();
                obj.Display();
                /* error CS0122: 'CodeSteps.Base.PrivateDisplay()' is inaccessible due to its protection level
                */
                //obj.PrivateDisplay();
            }
        }
    };
}

The above code has the comments and self-explanatory. So, I am not explaining about the code here. If you need any clarifications, please post a comment here.

We are done with ‘C#’ classes here. If I need to cover any things related to ‘C#’ classes, I will cover them through separate articles.

(Raju)

C#: Classes – Access Specifiers & Inheritance

Leave a Reply

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

Scroll to top