Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 12-2 extracted from chapter Inheritance, polymorphism and abstraction


Listing 12-1<     > Listing 12-3


This listing can be compiled with the command line:
csc.exe /target:exe Example_12_2.cs
Errors: 0 Warnings: 0


Example_12_2.cs
public class Employee {
   // The field m_Name is visible from derived classes.
   protected string m_Name;
   public Employeestring name ) { m_Name = name; }
   public virtual void DisplayDescription() {
      System.Console.Write( "Name: {0}", m_Name );
   }
}
class Secretary : Employee {  // Secretary inherits Employee.  
   public Secretarystring name ) : base( name ) {}
   public override void DisplayDescription() {
      // Call the Employee version of the DisplayDescription() method.
      base.DisplayDescription(); 
      System.Console.Write( "   Position: Secretary\n" );
   }
}
class Technician : Employee {  // Technician inherits Employee.  
   public Technicianstring name ) : base( name ) {}
   public override void DisplayDescription(){
      // Call the Employee version of the DisplayDescription() method.
      base.DisplayDescription(); 
      System.Console.Write( "  Position: Technician\n");
   }
}
class Program {   
   static void Main() {
      Employee[] array = new Employee[3];
      array[0= new Technician("Line");
      array[1= new Secretary("Lisanette");
      array[2= new Secretary("Anne-Mette");
      foreachEmployee employee in array )
         employee.DisplayDescription();
   }
}	
Copyright Patrick Smacchia 2006 2007