Class Base
Class Base
{
}
The operator ':' is used to indicate that a class is inherited from another class.
Remember that in C#, a derived class can't be more accessible than it's base class.
That means that it is not possible to declare a derived class as public, if it inherits
from a private class. For example the following code will generate a compile time
error.
class Base
{
}
In the above case the Base class is private. We try to inherit a public class from a
private class.
In this case Derived class inherits public members of the Base class x,y and
Method(). The objects of the Derived class can access these inherited members along
with its own member z.
// Inheritance
using System;
class Base
{
public int x = 10;
public int y = 20;
public void Method()
{
Console.WriteLine("Base Method");
}
}
class Derived : Base
{
public int z = 30;
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
Console.WriteLine("{0},{1},{2}", d1.x, d1.y, d1.z); // displays 10,20,30
d1.Method(); // displays 'Base Method'
}
}
A derived class inherits every thing from the base class except constructors and
destructors. The public members of the Base class becomes the public members of
the Derived class also. Similarly the protected members of the base class become
protected members of the derived class and internal member becomes internal
members of the derived class. Even the private members of the base class are
inherited to the derived class, even though derived class can't access them.
We know all base class data members are inherited to the derived, but their
accessibility remains unchanged in the derived class. For example in the program
given below
// Inheritance : datamebers
using System;
class Base
{
public int x = 10;
public int y = 20;
}
class Derived : Base
{
public int z = 30;
public void Sum()
{
int sum = x + y + z;
Console.WriteLine(sum);
}
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1.Sum();// displays '60'
}
}
Here class derived have total three data members, two of them are inherited from
the Base class.
In C#, even it is possible to declare a data member with the same name in the
derived class as shown below. In this case, we are actually hiding a base class data
member inside the Derived class. Remember that, still the Derived class can access
the base class data member by using the keyword base.
// Inheritance : datameber hiding
using System;
class Base
{
public int x = 10;
public int y = 20;
}
class Derived : Base
{
public int x = 30;
public void Sum()
{
int sum = base.x + y + x;
Console.WriteLine(sum);
}
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1.Sum();// displays '60'
}
}
But when we compile the above program, the compiler will show a warning, since we
try to hide a Base class data member inside the Derived class. By using the keyword
new along with the data member declaration inside the Derived class, it is possible to
suppress this compiler warning. The keyword new tells the compiler that we are
trying to explicitly hiding the Base class data member inside the Derived class.
Remember that we are not changing the value of the Base class data member here.
Instead we are just hiding or shadowing them inside the Derived class. However the
Derived class can access the base class data member by using the base operator.
What ever we discussed for data members are valid for member functions also. A
derived class member function can call the base class member function by using the
base operator. It is possible to hide the implementation of a Base class member
function inside a Derived class by using the new operator. When we declare a method
in the Derived class with exactly same name and signature of a Base class method, it
is known as 'method hiding'. But during the compilation time, the compiler will
generate a warning. But during run-time the objects of the Derived class will always
call the Derived class version of the method. By declaring the derived class method
as new, it is possible to suppress the compiler warning.
Uses of new and base operators are given in the following program.
The constructors and destructors are not inherited to a Derived class from a Base
class. However when we create an object of the Derived class, the derived class
constructor implicitly call the Base class default constructor. The following program
shows this.
// Inheritance : constructor
using System;
class Base
{
public Base()
{
Console.WriteLine("Base class default constructor");
}
}
class Derived : Base
{
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();// Displays 'Base class default constructor'
}
}
Remember that the Derived class constructor can call only the default constructor of
Base class explicitly. But they can call any Base class constructor explicitly by using
the keyword base.
class Base
{
public virtual void Method()
{
}
}
When we declare a virtual method, it must contain a method body. Other wise the
compiler will generate an error. Remember that, since virtual methods are used for
achieving polymorphism and since polymorphism works only with objects, it not
possible to declare a static method as virtual in C#. Similarly the private methods
are also not possible to declare virtual, since they can't override inside a derived
class.
In C#, it is not necessary to override a Base class virtual method inside a Derived
class.
using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1.Method(); // Displays 'Base Method'
}
}
using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
public virtual void Method()
{
Console.WriteLine("Derived method");
}
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1.Method(); // Displays 'Derived Method'
}
}
Even it is possible to omit the keyword virtual from the Derived class method or it is
possible to declare the Derived class method as new.
In C#, a Base class reference can hold an object of the Derived class and when it
invokes the methods, it will invoke always the Derived class methods, if you already
override that method inside the Derived class by using the override keyword. Also
the Base class method must declare using the keyword virtual. This is what is known
as Polymorphism in C#.
As with a virtual method, we must include a method body with an override method;
other wise the compiler will generate an error during compilation.
Remember that in C#, we can use the override keyword with only virtual method,
when overriding inside a Derived class. An overridden declaration must be identical in
every way to the virtual method, it overrides. They must have the same access level;
the same return type, the same name and same method signature. The overridden
methods are implicitly virtual in nature and hence they can override in subsequent
Derived classes. As like virtual methods, override methods can't be declared as static
or private.