Inheritance in C#

This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.

Inheritance is a relationship that defines one entity in terms of another. Class inheritance defines a new class in terms of one parent class or one or more interfaces. The new class inherits its interface and implementation from its parent class and method signatures. The new class is called a subclass or a derived class.

Class inheritance combines interface inheritance and implementation inheritance. Interface inheritance defines a new interface in terms of one or more existing interfaces. Implementation inheritance defines a new implementation in terms of one or more existing implementations.

Classes in C# support only single inheritance, and Object is the ultimate base class for all classes. The classes shown in earlier examples all implicitly derive from Object.

Access modifiers in a classes members give different levels of access to derived classes. Below is a table that describes the access a child class has to the members of the inherited class depending upon the access modifier.


access-specifier: public


Parent class access modifier Access in child class public accessible protected accessible private not accessible

table5.6.gif

Table 5.6: Access-Specifier Members and inheritance scope

C# only makes use of public inheritance, meaning that you cannot specify an inheritance modifier in C#. (Refer to the C# Language Specification, available at http://msdn.microsoft.com/net/ecma/, for more details and recent updates to the C# language.) Listing 5.50 shows an example of a class deriving inheritance with public access specified.

Listing 5.50: Inheritance Example 1


using
System;

class
A // derived from object behind the scenes
{
    public void F() { Console.WriteLine("A.F"); }
}


class
B : A // B inherits A
{
    public void G() { Console.WriteLine("B.G"); }
}


class
Test
{
    static void Main()
    {
        B b = new B();
        b.F(); // Inherited from A
        b.G(); // Introduced in B
        A a = b; // Treat a B as an A, polymorphic access
        a.F();
        Console.ReadLine();
    }
}


Listing 5.51: Inheritance Example 2


// example1 protected

class
A
{
    protected int x = 123;
}


class
B : A
{
    void F()
    {
        A a = new A();
        B b = new B();
        a.x = 10; // Error
        b.x = 10; // OK
    }
}


Listing 5.52 shows another example of private inheritance that enables direct access to protected members.

Listing 5.52: Protected.cs, Protected Modifier Example


// example2 protected


using
System;

class
MyClass
{
    protected int x;
    protected int y;
}


class
MyDerivedC : MyClass //private inheritance
{
    public static void Main()
    {
        MyDerivedC mC = new MyDerivedC();
        // Direct access to protected members:
        mC.x = 10;
        mC.y = 15;
        Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y);
        Console.ReadLine();
    }
}


Figure 5.12 contains the resulting screen output.

fig-5.12.gif

Figure 5.12: Screen Output Generated from Listing 5.52

Conclusion

Hope this article would have helped you in Inheritance in C#. See other articles on the website on .NET and C#.

visual C-sharp.jpg
The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.

Up Next
    Ebook Download
    View all
    FileInfo in C#
    Read by 9.7k people
    Download Now!
    Learn
    View all