Next Page
Inheritance & Polymorphism in C#
If you are new to C# School
This is the 5th in a series of lessons in our C# School. The C# School is a kind of interactive learning platform where those who want to learn .NET with C# can find help and support. With one issue a week, describing some areas of the C# Programming Language with the Microsoft .Net Platform, this is not the same traditional passive tutorial where the author only writes and the reader only reads. There will be exercise problems at the end of each issue, which the reader is expected to solve after reading the issue. The solution to these problems will be provided in the next issue for testing purposes. There is also a dedicated
message board attached with the school, where you can ask questions about the article, and the author will respond to your question within 2/3 days. You can send your suggestions, feedback or ideas on how these lessons can be improved to either the Author (
farazrasheed@acm.org)or the WEBMASTER (
info@programmersheaven.com).
For previous lessons:
Lesson Plan
Today we will learn fundamental object oriented features like inheritance and polymorphism in C#. We will start by building on our understanding of inheritance and then we will move towards understanding how C# supports inheritance. We will spend some time exploring the object class and then we will go towards polymorphism and how polymorphism is implemented in C#. We will finish this lesson by understanding how boxing, un-boxing and type-casting mechanisms work in C#.
Inheritance
Unless this is your first time at object oriented programming, you will have heard a lot about Reusability and Extensibility. Reusability is the property of a module (a component, class or even a method) that enables it to be used in different applications without any or little change in its source code. Extensibility of a module is it's potential to be extended (enhanced) as new needs evolve. Reusability in Object Oriented Programming languages is achieved by reducing coupling between different classes, while extensibility is achieved by sub-classing. The process of sub-classing a class to extend its functionality is called Inheritance or sub-typing.
The original class (or the class that is sub-typed) is called the base, parent or super class. The class that inherits the functionality of the base class and extends it in its own way is called the sub, child, derived or inherited class.
http://www.programmersheaven.com/articles/faraz/lesson5_img1.gif
In the figure above, we have used a UML (Unified Modeling Language) class diagram to show Inheritance. Here, Shape is the base class while Circle, Rectangle and Curve are its sub-classes.
A base class usually has general functionality, while sub-classes possess specific functionality. So, when sub-classing or inheriting, we go
'from specialization to generalization'.
If a class B (sub class) inherits a class A (base class), then B would have a copy of all the instance members (fields, methods, properties) of class A and B can access all the members (except for the private members) of class A. Private members of a base class do get inherited in a sub-class, but they can not be accessed by the sub-class. Also, inheritance is said to create a,
'type of' relationship among classes which means sub-classes are a type of base class. (If you are not getting the idea, don't worry, things will get clearer when we implement inheritance in the following sections)
Inheritance in C#
Before we go on to implementation, here are some key-points about inheritance in C#
- C#, like Java and contrary to C++, allows only single class inheritance. Multiple inheritance of classes is not allowed in C#.
- The Object class defined in the System namespace is implicitly the ultimate base class of all the classes in C# (and the .NET framework)
- Interfaces in C# can inherit more than one interface. So, multiple inheritance of interfaces is allowed in C# (again similar to Java). We will look at interfaces in detail in the coming lessons.
- Structures (struct) in C# can only inherit (or implement) interfaces and can not be inherited.
Author's Note: Technically, a class inherits another class, but implements an interface. It is technically wrong to say that class A inherits interface B, rather, it should be like,
'class A implements interface B'. Although, many write-ups don't follow this, I would recommend using the word 'inherits' only where it makes sense.
Next Page