Inheritance Example


Before introducing the specifics of inheritance, an example that includes all the prerequisite elements of inheritance might be helpful. In the code, XParent is the base class. XChild is the derived class and inherits the XParent class. XChild inherits a method, property, and field from the base class. XChild extends XParent by adding a method and field to this assemblage. XChild has five members: three from the base and two from itself. In this manner, XChild is a specialty type and refines XParent. In Main, instances of the XParent and XChild classes are created. Base methods are called on the XParent instance. Both base and derived methods are called on the XChild instance.

 using System; namespace Donis.CSharpBook{     public class Starter{         public static void Main(){             XParent parent=new XParent();             parent.MethodA();             XChild child=new XChild();             child.MethodA();             child.MethodB();             child.FieldA=10;             Console.WriteLine(child.FieldA);         }         public class XParent {             public void MethodA() {                 Console.WriteLine("XParent.MethodA called from {0}.",                     this.GetType().ToString());             }             private int propFieldA;             public int FieldA {                 get {                   return propFieldA;                 }                 set {                     propFieldA=value;                 }             }         }         public class XChild: XParent {             public int MethodB() {                 Console.WriteLine("XChild.MethodB called from {0}.",                     this.GetType().ToString());                 return fieldb;             }             private int fieldb=5;         }     } } } 

Cross-Language Inheritance

Inheritance is language-agnostic. Managed languages can inherit classes written in another managed language. For library developers, this expands the universe of potential clients. Developers no longer need to maintain language-specific versions of a library or create complex workarounds. Just as important, a family of developers is not excluded from using a certain library. Another benefit of cross-language inheritance is collaboration. Team members collaborating on a software system can develop in the language of their choice. The entire team is not compelled to select a single source language.

Managed languages compile to Microsoft intermediate language (MSIL) code. The Common Language Runtime (CLR) does not perceive a Microsoft Visual Basic .NET class inheriting from a C# class. It views one MSIL class inheriting from another MSIL class. Language independence is easier to achieve when specific languages dissolve into a shared common language at compilation.

Cross-language inheritance fractures without compliance to the Common Language Specification (CLS)—at least relative to the base or derived class. Language-specific and noncompliant artifacts must be wrung from classes when cross-language inheritance is planned or expected. For example, the following class, although perfectly okay in C#, is unworkable in Visual Basic .NET. Visual Basic .NET is case insensitive, making MethodA in the following code ambiguous:

 public class XBase {     public void MethodA() {     }     public void methoda() {     } } 

The following code is an example of successful cross-language inheritance. The base class is written in C#, whereas the derived class is Visual Basic .NET.

 ' VB Code: which includes derived class. Imports System Imports Donis.CSharpBook Namespace Donis.CSharpBook     Public Class Starter         Public Shared Sub Main             Dim child as New XChild             child.MethodA()             child.MethodB()         End Sub     End Class     Public Class XChild         Inherits XParent         Public Sub MethodB             Console.WriteLine("XChild.MethodB called from {0}.", _                 Me.GetType().ToString())         End Sub     End Class End Namespace // C# Code: which includes base class using System; namespace Donis.CSharpBook{     public class XParent {         public void MethodA() {             Console.WriteLine("XParent.MethodA called from {0}.",                 this.GetType().ToString());         }         private int propFieldA;         public int FieldA {             get {                     return propFieldA;             }             set {                     propFieldA=value;             }         }     } } 




Programming Microsoft Visual C# 2005(c) The Language
Microsoft Visual Basic 2005 BASICS
ISBN: 0619267208
EAN: 2147483647
Year: 2007
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net