Demonstrate basic inheritance - CSharp Custom Type

CSharp examples for Custom Type:Inheritance

Description

Demonstrate basic inheritance

Demo Code

using System;/*  ww  w.  java  2 s . c  o  m*/
public class Dog {
   //Common attributes
   public string Name;
   public int    Weight;
   //Common methods
   public void Speak( ) { Console.WriteLine("ruff!"); }
   public void DrinkWater( ) { Console.WriteLine("Gulp"); }
}
//A specialized version of a Dog
public class GermanShepard : Dog {
   public void OnGuard( ) { Console.WriteLine("In Guard Mode"); }
}
public class JackRussell : Dog {
   public void Chew( ) { Console.WriteLine("I'm chewing your favorite shoes!"); }
}
public class Inherit {
   public static void Main( ) {
      GermanShepard A = new GermanShepard( );
      JackRussell Daisy   = new JackRussell( );

      A.Name = "A"; A.Weight = 85;
      Daisy.Name = "Daisy"; Daisy.Weight = 25;

      A.Speak( ); A.DrinkWater( );
      Daisy.Speak( ); Daisy.DrinkWater( );

      A.OnGuard( );
      //Only Diasy has the Chew method
      Daisy.Chew( );
   }
}

Result


Related Tutorials