C# - Basics - Classes - Inheritance - Implementation Inheritance - Introduction
Mart 10, 2011 by C# Tutorial
|
|
Inheritance in C#
This topic contains and tries to cover following subjects:
- Introduction to Inheritance C#
- Explanation and differences of Inheritance types in C#
- Difference of implementation Inheritance (derived from another Class) from Interface Inheritance (Deriving a Class) from Interface in C#
- A basic example of Implementation Inheritance (Derived Classes)
Explanation of Inheritance in C#
In C#, similar to the C++, supports inheritance.
What is inheritance and why it is used:
A designed Class, with inheritance, can provide its methods-fields or similar members to another Class. The Derived Class (which is inherited from a Base Class), in term, with inheriting a base Class, gains features of base Class. In another way, a derived Class may need to extend-advance function of a base Class with adding its own members.
A basic-simple inheritance example:
For scenario to explain inheritance of a Class, we have a hardware Management application with 3 Class.
- 1 Base Class "Computers" have a field for hardware ID to uniquely identify each hardware.
- 2 Derived Class "Notebooks" inherits "Computers" Class, and with inheriting it, it gains hardwareID field automatically. "Desktops" Class is the same. When you consider a bigger Class, applications are designed to reflect its inner proccess with their Class inheritance. It is demonstrated in the following image.
.NET framework Classes are a good example of inheritance which uses similar logic. All Classes of .NET Framework inherits System.Object Cass. When you create an object-instance of your own Class or .NET framework native Class, you may notice that the objects-instance you created supports some basic methods. "ToString()" method for example is available by default to all Class Objects. .NET framework Classes inherit System.Object Class by default. Image from compiler indicates inheritance of System.Object to a user-created Class.
Implementation Inheritance Syntax in C#
to derive a Class from a base Class, following semicolon the Base Class name is added.
Syntax:
class baseClassName
{}
class derivedClassName : baseClassName
{}
Implementation Inheritance Example for C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NS_hardwareManagerApp
{
//declare a base Class...///
class CL_hardwares
{
public string field_hardwareID;
}
//declare a derived Class and inherit base Class...///
class CL_notebooks : CL_hardwares
{
public string field_batteryType;
}
class CL_x
{
static void Main(string [] args)
{
//create an instance of Class...///
CL_notebooks o_aNotebook = new CL_notebooks();
//assign values to fields of Instance...///
o_aNotebook.field_hardwareID = "1";
o_aNotebook.field_batteryType = "lithium";
//display fields...///
Console.WriteLine("ID: " + o_aNotebook.field_batteryType);
Console.WriteLine("battery type: " + o_aNotebook.field_batteryType);
}
}
}
Console output:
As it is indicated above console application source code, we inherited a base Class. Due to inheritance, we only added additional fields which was required in derived Class. ID fields came automatically in derived Class. Since ID was logically a requirement of our notebook instances.
Data Layers
Area: | programming \ Languages \ csharp \ \ \ |
Ref: | http://msdn.microsoft.com/en-us/library/ms173149.aspx |
Loc: | articles |
Tags: | csharp |
|