CSharp - Call method from parent

Introduction

Consider the following code

Demo

using System;

class ParentClass
{
    public void ShowParent()
    {// w ww .  j  a v a2 s  .c  om
        Console.WriteLine("In Parent");
    }
}
class ChildClass : ParentClass
{
}
class Program
{
    static void Main(string[] args)
    {
        //Testing Inheritance
        ChildClass child1 = new ChildClass();
        //Invoking ShowParent()through ChildClass object 
        child1.ShowParent();
    }
}

Result

Note

We have invoked the ShowParent() method through a child class object.

In C#, object(System.Object) is the root for all classes.

System.Object is the ultimate base class in the type hierarchy.

Apart from constructors and destructors, all members are inherited.

Due to their accessibility restrictions, all the inherited members may not be accessible in the child/derived class.

The child class can add new members but it cannot remove the definition of the parent member.

The inheritance hierarchy is transitive; if class C inherits class B, which in turn is derived from class A, then class C contains all the members from class B and class A.

Related Topic