Home‎ > ‎LEARN C #‎ > ‎

C# - Inheritance

Inheritance is one of the main features of an object-oriented language. C# and the .NET class library are heavily based on inheritance. The telltale sign of this is that all .NET common library classes are derived from the object class, discussed at the beginning of this article. As pointed out, C# doesn’t support multiple Inheritance. C# only supports single inheritance; therefore, all objects are implicitly derived from the object class.
Implementing inheritance in C# is similar to implementing it in C++. You use a colon (:) in the definition of a class to derive it from another class. 

In the example blow BaseClassB, which later accesses the BaseClassA method in the Main method. 
Inheritance example
using System;
// Base class A
class BaseClassA
{
public void MethodA()
{
Console.WriteLine("A Method called");
}
}
// Base class B is derived from Base class A
class BaseClassB:BaseClassA
{
public void MethodB()
{
Console.WriteLine("B method called");
}
}
class myClass
{
static void Main()
{
// Base class B
BaseClassB b = new BaseClassB();
// Base class B method
b.MethodB();
// BaseClassA Method through BaseClassB
b.MethodA();
}
}




                       HOME                                          PREVIOUS                                



      
Comments