There are two types of inheritence implemented in C#. Implementation Inheritence and Interface Inheritence.
Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and functions. With implementation inheritance, a derived type adopts the base type’s implementation of each function, unless it is indicated in the definition of the derived type that a function implementation is to be overridden. This type of inheritance is most useful when you need to add functionality to an existing type, or where a number of related types share a significant amount of common functionality.
Interface inheritance means that a type inherits only the signatures of the functions but does not inherit any implementations. This type of inheritance is most useful when you want to specify that a type makes certain features available. Interface inheritance is often regarded as providing a contract: By deriving from an interface, a type is guaranteed to provide certain functionality to clients.
Imp: C# doesnot implement Multiple Inheritence i.e. you cannot inherit a class from more than one class.
By declaring a base class function as virtual, you allow the function to be overridden in any derived classes.
class MyBaseClass
{
public virtual string VirtualMethod()
{
return “This method is virtual and defined in MyBaseClass and can be overridden in derievd class”;
}
}
You use the override keyword when you write a new implementation of the virtual method in the derieved class.
public override string VirtualMethod() //this is the method which is overriden.
Neither member fields nor static functions can be declared as virtual.
Hiding Methods
If a method with the same signature is declared in both base and derived classes, but the methods are not declared as virtual and override, respectively, then the derived class version is said to hide the base class version.In these situations, C# generates a compilation warning. That reminds you to use the new keyword to declare that you intend to hide a method.
you can use the base.<MethodName>() syntax to call any method in the base class .
C# allows both classes and functions to be declared as abstract. An abstract class cannot be instantiated, whereas an abstract function does not have an implementation, and must be overridden in any non-abstract derived class. Obviously, an abstract function is automatically virtual (although you don’t need to supply the virtual keyword; doing so results in a syntax error).
C# allows classes and methods to be declared as sealed. In the case of a class, this means that you can’t inherit from that class. In the case of a method, this means that you can’t override that method.
Constructors in the hierarchy
If all the classes in the hierarchy have only default constructors then when u instantiate a class the control goes to the constructor of its immediate base class from there it goes to its immediate base class…like that till it reaches System.Object. Since System.Object doesnot have any base class…its constructor is executed…then its next derieved class’ cons is exec…till it reaches the cons of the class to be instantiated.
When you write your own constructors…stick to this principle.The base and this keywords are the only keywords allowed in the line that calls another constructor. Anything else causes a compilation error. Also note that only one other constructor can be specified.
We’ll take an example to understand this:
We have class called Employee it just has a field indicating the EmpId
class Employee
{
private string empId;
public Employee()
{
}
}
Then you have a class called ContractEmployee which is inherited from Employee and has an extra field of number of contract months.
class ContractEmployee: Employee
{
private int contractMonths;
public ContractEmployee():base()
{
}
}
In the above case when the class ContractEmployee is instantiated then the default cons of its base class is called and goes upto Object class. If you want to instantiate the employee field with employee id i.e if the user has to produce the employee id of the new emp while creating the class itself then the constructor of the Employee class takes a parameter and takes the following form
class Employee
{
private string empId;
public Employee(string id)
{
this.empId = id;
}
}
Now when you instantiate the derieved class i.e the ContractEmployee class then you have to supply name to the emp…but that field is a private member of the base class. So what you have is sth like this:
class ContractEmployee: Employee
{
private int contractMonths;
public ContractEmployee(string id):base(id)
{
}
}
The derieved class has no way to implement this but the base class can do that so what the former does is to pass on the parameter to the base class cons.
Now the req is that the emp who referred to the conractemp should be saved in a field of referrer.
class ContractEmployee: Employee
{
private int contractMonths;
private string referrer;
public ContractEmployee(string id,string refr):base(id)
{
this.referrer = refr
}
}
But there may be some contract emp who have no referrers for them there would be no referrers. For them you should have a one parameter cons.Note how this is implemented….the derieved class now has two constructors with two parameters.
class ContractEmployee: Employee
{
private int contractMonths;
private string referrer;
public ContractEmployee(string id,string refr):base(id)
{
this.referrer = refr
}
public ContractEmployee(string id):this(id,”none”)
{
}
}
thi is how more than one constructors are implemented.When a contract emp with no ref is instantiated then cons with one param is called which in turns call the cons with 2 params giving the referrer as “none” then the base classes single param cons is called.