Wednesday, July 1, 2009

Inheritance in C#

Inheritance in C#
This article discusses Inheritance concepts in the context of C#. Before we understand Inheritance in C# it is important to understand the key players involved, viz. Objects, Classes and Structs.
Classes and Structs are ‘blue-prints’ or templates from which we instantiate (create) objects. Example a car may be created based on its blue print. Car is the object and blue print is the class (or template).
What are types?
An object can be of the following types – Class or Struct. There are many differences between the two ‘types’. The main difference between the two is the way in which they are stored in memory and the way they are accessed. Classes are also called reference types Structs are known as value types. Classes are stored in a memory space called ‘heap’ and Structs are stored in a memory space known as ‘stack’
Constructors:
In C#, (like other Objected Oriented languages) constructor is a method having the same name as the class. The constructor is called when the object is being created. It can have one or more parameters.
Interfaces:
In the context of C#, an interface provides a contract. A class that is derived from this interface will implement the functions specified by the interface.
Inheritance:
C# supports two types of Inheritance mechanisms 1) Implementation Inheritance 2) Interface Inheritance
What is Implementation Inheritance?
- When a class (type) is derived from another class(type) such that it inherits all the members of the base type it is Implementation Inheritance
What is Interface Inheritance?
- When a type (class or a struct) inherits only the signatures of the functions from another type it is Interface Inheritance.
In general Classes can be derived from another class, hence support Implementation inheritance. At the same time Classes can also be derived from one or more interfaces. Hence they support Interface inheritance. Structs can derive from one more interface, hence support Interface Inheritance. Structs cannot be derived from another class they are always derived from System.ValueType
Multiple Inheritance:
C# does not support multiple implementation inheritance. A class cannot be derived from more than one class. However, a class can be derived from multiple interfaces.
Inheritance Usage Example: Here is a syntax example for using Implementation Inheritance.
Class derivedClass:baseClass { }
derivedClass is derived from baseClass.
Interface Inheritance example:
private Class derivedClass:baseClass , InterfaceX , InterfaceY{ }
derivedClass is now derived from interfaces – InterfaceX, InterfaceY Similarly a struct can be derived from any number of interfaces
private struct childStruct:InterfaceX, InterfaceY{}
Virtual Methods: If a function or a property in the base class is declared as virtual it can be overridden in any derived classes Usage Example:
class baseClass { public virtual int fnCount() { return 10;} } class derivedClass :baseClass {public override int fnCount() { return 100;} }

This is useful because the compiler verifies that the ‘override’ function has the same signature as the virtual function
Hiding Methods: Similar to the above scenario if the methods are declared in a child and base class with the same signature but without the key words virtual and override, the child class function is said to hide the base class function
class someBaseClass { } class abcClass:someBaseClass {public int fnAge() {return 99;} } class grandchildClass: abcClass{public int fnAge() { return 10;} }

No comments: