Sponsored Links
C Sharp Tutorials
- Structural and Behavioral Design Patterns
- Creational Design Patterns
- Software Architecture & Design Patterns
- C# Language Basics
- . NET Type Safety
- Regular Expressions in C# - Quantifiers and Delegates
- Regular Expressions and C#, .NET
- .NET Remoting
- Inheritance in C#
- Delegates in C#
- Building Web Based N-Tier Applications using C#
Tutorials
C SharpInheritance in C#
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 SystemValueType
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;
}
}
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
Comments
Stein said:
| Provided a quick and succinct review of inheritance in c#. |
Subramaniyan T said:
| Excellent Job by exforsys |
Shailesh Wath said:
|
Exforsys has done good work for beginners |
Priyap said:
| Very good |
Amandeep Singh Maan said:
| 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 |
Md Salman Arafath said:
| its good |
Ali Mardan said:
| i have gone through this whole article, its really good work. i really appriciate the way how author wrote about small and key concepts. |
Manobala said:
| gud one ......simple and good |
Muhammad Aqeel Akram said:
| Article is good and one can easily recals his/her basic concepts of Objected Oriented Programming specific to C#. |
Khushi Desai said:
| good one. keep it up |
Puneet Goyal said:
|
But it creates lot of confidence in me. Thanks keep Writing. |
Varsha Yadav said:
| i have gone through this whole article, its really good work. i really appriciate the way how author wrote about small and key concepts. |
Hitesh Desai said:
| very good.... keep it up |
khaled mohamed said:
|
thanks alot. great tutorial |
Nick said:
|
Very nice and so fast learning! |
karthikeyan.L said:
| i need it with example.... |
Changalrao said:
| very good |
1 said:
| please Give information about every one letest change in C# |
Ashish Sehra said:
| Gr8 Article You have picked a topic and given an example this is superb |
Nitin Shitole said:
| superb |
lalit said:
| thats very good for easy learning |
kavikannan said:
| This is very nice and easy to learn. Thanks to Exforsys Inc team |
Sponsored Links
