Free Training
C Language   |   CSS   |   MainFrame   |   VBScript   |   PHP   |   XML   |   C++ Tutorials   |   Ajax   |   JavaScript   |   CSS3   |   UML   |   jQuery   |   Microsoft AJAX

Sponsored Links

C Sharp Tutorials

 
Home Tutorials C Sharp
 

Inheritance in C#

 

Inheritance in C#

Page 1 of 2

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


Read Next: Regular Expressions and C#, .NET



 

 

Comments


Stein said:

  Provided a quick and succinct review of inheritance in c#.
February 10, 2007, 10:41 pm

Subramaniyan T said:

  Excellent Job by exforsys
March 8, 2007, 2:59 am

Shailesh Wath said:

  Exforsys has done good work for beginners
May 5, 2007, 5:58 am

Priyap said:

  Very good
October 9, 2007, 3:47 am

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
October 10, 2007, 7:55 am

Md Salman Arafath said:

  its good
November 8, 2007, 4:35 am

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.
December 17, 2007, 11:59 pm

Manobala said:

  gud one ......simple and good
December 21, 2007, 12:27 am

Muhammad Aqeel Akram said:

  Article is good and one can easily recals his/her basic concepts of Objected Oriented Programming specific to C#.
March 10, 2008, 2:42 am

Khushi Desai said:

  good one. keep it up
April 30, 2008, 2:02 am

Puneet Goyal said:

  But it creates lot of confidence in me.
Thanks
keep Writing.
May 5, 2008, 1:21 am

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.
May 19, 2008, 7:05 am

Hitesh Desai said:

  very good.... keep it up
June 3, 2008, 4:51 am

khaled mohamed said:

  thanks alot.
great tutorial
June 12, 2008, 6:58 am

Nick said:

  Very nice and so fast learning!
August 11, 2008, 11:42 am

karthikeyan.L said:

  i need it with example....
November 23, 2008, 11:27 pm

Changalrao said:

  very good
December 12, 2008, 10:48 pm

1 said:

  please Give information about every one letest change in C#
December 31, 2008, 12:20 am

Ashish Sehra said:

  Gr8 Article You have picked a topic and given an example this is superb
January 2, 2009, 12:44 pm

Nitin Shitole said:

  superb
February 19, 2009, 6:32 am

lalit said:

  thats very good for easy learning
February 24, 2009, 1:08 am

kavikannan said:

  This is very nice and easy to learn. Thanks to Exforsys Inc team
April 26, 2009, 2:51 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links