Method override 3 : Override Virtual « Class Interface « C# / C Sharp






Method override 3

Method override 3
/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 class Dog
 {
     private int weight;

     // constructor
     public Dog(int weight)
     {
         this.weight = weight;
     }

     // override Object.ToString
     public override string ToString()
     {
         return weight.ToString();
     }
 }


 public class TesterOverride
 {
     static void Main()
     {
         int i = 5;
         Console.WriteLine("The value of i is: {0}", i.ToString());

         Dog milo = new Dog(62);
         Console.WriteLine("My dog Milo weighs {0} pounds", milo.ToString());
     }
 }

           
       








Related examples in the same category

1.Polymorphism
2.Virtual keyword can be used to start a new inheritance ladder
3.Virtual and overloadVirtual and overload
4.Class hierarchy: override and virtual
5.Use virtual methods and polymorphismUse virtual methods and polymorphism
6.Demonstrate a virtual methodDemonstrate a virtual method
7.When a virtual method is not overridden, the base class method is usedWhen a virtual method is not overridden, 
   the base class method is used
8.illustrates polymorphismillustrates polymorphism
9.Demonstrates the use of a virtual method to override a base class methodDemonstrates the use of a virtual method to override a base class method
10.Demonstrates the use of a virtual property to override a base class propertyDemonstrates the use of a virtual property to override a base class property
11.Test Polymorphism Virtual Functions