Tutorials
C SharpThis 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)
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’
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
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
C# supports two types of Inheritance mechanisms
1) Implementation Inheritance
2) Interface 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
- 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 SystemValueType
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;
}
}
In the example above the function fnAge in grandChildClass hides the function fnAge in its parent class ie abcClass
The C# compiler will generate a warning in this case The new keyword should be used when we intend to hide a method
Example:
class grandchildClass: abcClass
{
public new int fnAge()
{
return 10;
}
}
Next Page: Inheritance in C# - Page 2
| Provided a quick and succinct review of inheritance in c#. |
| Excellent Job by exforsys |
|
Exforsys has done good work for beginners |
| Very good |
| youhave done a good job. it's an faster review of all type of inheritance we can do. really it's an appreciateable job................... GOOD WORK |
| its good |
| i have gone through this whole article, its really good work. i really appriciate the way how author wrote about small and key concepts. |
| gud one ......simple and good |
| Article is good and one can easily recals his/her basic concepts of Objected Oriented Programming specific to C#. |
| good one. keep it up |
|
But it creates lot of confidence in me. Thanks keep Writing. |
| i have gone through this whole article, its really good work. i really appriciate the way how author wrote about small and key concepts. |
| very good.... keep it up |
|
thanks alot. great tutorial |
|
Very nice and so fast learning! |
| i need it with example.... |
| very good |
| please Give information about every one letest change in C# |
| Gr8 Article You have picked a topic and given an example this is superb |