Entry
C#: Class: Inheritance: Create: Simple: Can you give an example of inheritance?
Jan 10th, 2004 11:01
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 27 September 2003 - 07:23 pm ------------------
C#: Class: Inheritance: Create: Simple: Can you give an example of
inheritance?
---
1. In general:
---
Steps: Overview:
---
1. -Create a class <yourclassnameparent>:
http://www.faqts.com/knowledge_base/view.phtml/aid/24304/fid/791
---
2. -Create another class <yourclassnamechild>:
1. To indicate that you want to use inheritance, use the following
notation:
class <yourclassnamechild> : <yourclassnameparent> {
// do something
// e.g. -add new variables, functions and procedures
//
// and or
//
// -overwrite existing variables, functions
// and procedures of that 'parent' class
};
2. So you indicate that your class is inheriting, by writing:
1. first the word 'class'
2. followed by the name of the 'child' class:
3. followed by a colon ':'
4. followed by the name of the 'parent' class
---
---
2. In specific:
---
1. -Create a parent class 'addressC',
and give the code for the procedures or functions used in
that class:
class addressC {
protected string nameS;
protected string streetS;
protected string cityS;
protected string countryS;
protected string telephoneS;
protected string emailS;
//
public void PROCView() {
Console.WriteLine( nameS + ' ' + '-' + ' ' + streetS + ' ' + '-
' + ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' +
telephoneS + ' ' + '-' + ' ' + emailS );
}
// the 'constructor' for the 'parent' class
public addressC( string nameS, string streetS, string cityS,
string countryS, string telephoneS, string emailS ) {
this.nameS = nameS;
this.streetS = streetS;
this.cityS = cityS;
this.countryS = countryS;
this.telephoneS = telephoneS;
this.emailS = emailS;
}
// the 'destructor' for the 'parent' class
~addressC() {
}
}
http://www.faqts.com/knowledge_base/view.phtml/aid/24776/fid/791
---
2. -Create another child class 'addresscompanyC',
and give the code for the procedures or functions used in
that class:
class addresscompanyC : addressC {
//
private string companynameS;
//
// a 'getter' for the 'child' class (you have to add the
word 'new')
public new void PROCView() {
Console.WriteLine( nameS + ' ' + '-' + ' ' + streetS + ' ' + '-
' + ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' +
telephoneS + ' ' + '-' + ' ' + emailS + ' ' + '-' + ' ' +
companynameS );
}
// the 'constructor' for the 'child' class (which calls
the 'constructor' of the 'parent' class first, this must be the first
line)
public addresscompanyC( string nameS, string streetS, string
cityS, string countryS, string telephoneS, string emailS, string
companynameS ) : base( nameS, streetS, cityS, countryS, telephoneS,
emailS ) {
this.companynameS = companynameS;
}
//
// the 'destructor' for the 'child' class
~addresscompanyC() {
}
}
---
3. -Declare the variables which you intend to use with your class:
addressC knudO = new addressC( "Knud", "my street 123", "my
city", "01-2345678", "my country", "myemail@address" );
addressC vanessaO = new addressC( "anothername", "anotherstreet
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );
//
addresscompanyC knudcompanyO = new addresscompanyC
( "myname", "mystreet 1", "mycity", "mycountry", "01-
2345678", "myemail@address", "thecompany1name" );
addresscompanyC vanessacompanyO = new addresscompanyC
( "anothername", "anotherstreet
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address", "thecompany2name" );
5. -Supply some source code where you use your class
(create another class, which contains the 'main()'
method. Then save this file e.g. with the same name
as that class (e.g. 'testinheritanceC.cs')
class testinheritanceC {
public static void Main() {
addressC knudO = new addressC( "Knud", "my street 123", "my
city", "01-2345678", "my country", "myemail@address" );
addressC vanessaO = new addressC
( "anothername", "anotherstreet
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );
//
addresscompanyC knudcompanyO = new addresscompanyC
( "myname", "mystreet 1", "mycity", "mycountry", "01-
2345678", "myemail@address", "thecompany1name" );
addresscompanyC vanessacompanyO = new addresscompanyC
( "anothername", "anotherstreet
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address", "thecompany2name" );
//
knudO.PROCView();
vanessaO.PROCView();
//
knudcompanyO.PROCView();
vanessacompanyO.PROCView();
}
}
---
4. -This gives alltogether the following source code:
--- cut here ---------------------------------------------------------
// save e.g. as 'testinheritance.cs'
using System;
// --- 1. declaration of the 'parent' class --- //
class addressC {
//
// it must be 'protected', because
// the 'child' class must be able to read their values
//
protected string nameS;
protected string streetS;
protected string cityS;
protected string countryS;
protected string telephoneS;
protected string emailS;
//
// --- 1.1 functions and procedures of the 'parent' class --- //
//
// 1.1.1 a getter for the 'parent' class
public void PROCView() {
Console.WriteLine( nameS + ' ' + '-' + ' ' + streetS + ' ' + '-
' + ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' +
telephoneS + ' ' + '-' + ' ' + emailS );
}
//
// 1.1.2 the 'constructor' for the 'parent' class
public addressC( string nameS, string streetS, string cityS,
string countryS, string telephoneS, string emailS ) {
this.nameS = nameS;
this.streetS = streetS;
this.cityS = cityS;
this.countryS = countryS;
this.telephoneS = telephoneS;
this.emailS = emailS;
}
//
// 1.1.3 the 'destructor' for the 'parent' class
~addressC() {
}
}
// --- 2. declaration of the 'child' class --- //
class addresscompanyC : addressC {
//
private string companynameS;
//
// --- 2.1 functions and procedures of the 'child' class --- //
//
// 2.1.1 a 'getter' for the 'child' class (you have to add the
word 'new')
public new void PROCView() {
Console.WriteLine( nameS + ' ' + '-' + ' ' + streetS + ' ' + '-
' + ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' +
telephoneS + ' ' + '-' + ' ' + emailS + ' ' + '-' + ' ' +
companynameS );
}
//
// 2.1.2 the 'constructor' for the 'child' class (which calls
the 'constructor' of the 'parent' class first, this must be the first
line)
// the 'constructor' for the 'child' class (which calls
the 'constructor' of the 'parent' class first, this must be the first
line)
public addresscompanyC( string nameS, string streetS, string
cityS, string countryS, string telephoneS, string emailS, string
companynameS ) : base( nameS, streetS, cityS, countryS, telephoneS,
emailS ) {
this.companynameS = companynameS;
}
//
// 2.1.3 the 'destructor' for the 'child' class
~addresscompanyC() {
}
}
// --- 3. do something --- //
class testinheritanceC {
public static void Main() {
//
// 3.1 create and initialize variables representing
that 'parent' class:
//
addressC knudO = new addressC( "Knud", "my street 123", "my
city", "01-2345678", "my country", "myemail@address" );
addressC vanessaO = new addressC
( "anothername", "anotherstreet
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );
//
// 3.2 create initialize variables representing that 'child'
class:
//
addresscompanyC knudcompanyO = new addresscompanyC
( "myname", "mystreet 1", "mycity", "mycountry", "01-
2345678", "myemail@address", "thecompany1name" );
addresscompanyC vanessacompanyO = new addresscompanyC
( "anothername", "anotherstreet
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address", "thecompany2name" );
//
// 3.3 do something
//
// 3.3.1 view variables stored in the variables representing
that 'parent' class:
//
knudO.PROCView();
vanessaO.PROCView();
//
// 3.3.2 view variables stored in the variables representing
that 'child' class:
//
knudcompanyO.PROCView();
vanessacompanyO.PROCView();
//
// finally the memory used by that variable will be released
(by C#), after use, when you do not need it anymore
}
}
--- cut here ---------------------------------------------------------
5. -When you run this source code, you will see e.g. a text screen
with the following information:
> csc.exe testinheritance.cs
+-------------------------------------------------------------------+
|Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4 |
|for Microsoft (R) .NET Framework version 1.1.4322 |
|Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.|
| |
|> testinheritance.exe |
| |
|Knud - my street 123 - my city - 01-2345678 - my country - |
|myemail@address |
| |
|anothername - anotherstreet 1 - anothercity - anothercountry - |
|01-3456789 - anotheremail@address |
| |
|myname - mystreet 1 - mycity - mycountry - 01-2345678 - |
|myemail@address - thecompany1name |
| |
|anothername - anotherstreet 1 - anothercity - anothercountry - |
|01-3456789 - anotheremail@address - thecompany2name |
| |
|Press any key to continue . . . |
+-------------------------------------------------------------------+
---
---
Note: this source code has been tested and worked OK
[kn, ni, mo, 29-09-2003 23:45:19]
---
---
Internet: see also:
---
C#: Class: Inheritance: What is inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24870
---
C++: Class: Inheritance: Create: Simple: Can you give an example of
inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24882/fid/163
---
Delphi: Class: Inheritance: Create: Simple: Can you give an example of
inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24880/fid/175
---
Java: Class: Inheritance: Create: Simple: Can you give an example of
inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24891/fid/165
----------------------------------------------------------------------