fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace OverrideAndNew2
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. // Declare objects of the derived classes and test which version
  13. // of ShowDetails is run, base or derived.
  14. TestCars1();
  15.  
  16. // Declare objects of the base class, instantiated with the
  17. // derived classes, and repeat the tests.
  18. TestCars2();
  19.  
  20. // Declare objects of the derived classes and call ShowDetails
  21. // directly.
  22. TestCars3();
  23.  
  24. // Declare objects of the base class, instantiated with the
  25. // derived classes, and repeat the tests.
  26. TestCars4();
  27. }
  28.  
  29. public static void TestCars1()
  30. {
  31. System.Console.WriteLine("\nTestCars1");
  32. System.Console.WriteLine("----------");
  33.  
  34. Car car1 = new Car();
  35. car1.DescribeCar();
  36. System.Console.WriteLine("----------");
  37.  
  38. // Notice the output from this test case. The new modifier is
  39. // used in the definition of ShowDetails in the ConvertibleCar
  40. // class.
  41. ConvertibleCar car2 = new ConvertibleCar();
  42. car2.DescribeCar();
  43. System.Console.WriteLine("----------");
  44.  
  45. Minivan car3 = new Minivan();
  46. car3.DescribeCar();
  47. System.Console.WriteLine("----------");
  48. }
  49. // Output:
  50. // TestCars1
  51. // ----------
  52. // Four wheels and an engine.
  53. // Standard transportation.
  54. // ----------
  55. // Four wheels and an engine.
  56. // Standard transportation.
  57. // ----------
  58. // Four wheels and an engine.
  59. // Carries seven people.
  60. // ----------
  61.  
  62. public static void TestCars2()
  63. {
  64. System.Console.WriteLine("\nTestCars2");
  65. System.Console.WriteLine("----------");
  66.  
  67. var cars = new List<Car> { new Car(), new ConvertibleCar(),
  68. new Minivan() };
  69.  
  70. foreach (var car in cars)
  71. {
  72. car.DescribeCar();
  73. System.Console.WriteLine("----------");
  74. }
  75. }
  76. // Output:
  77. // TestCars2
  78. // ----------
  79. // Four wheels and an engine.
  80. // Standard transportation.
  81. // ----------
  82. // Four wheels and an engine.
  83. // Standard transportation.
  84. // ----------
  85. // Four wheels and an engine.
  86. // Carries seven people.
  87. // ----------
  88.  
  89. public static void TestCars3()
  90. {
  91. System.Console.WriteLine("\nTestCars3");
  92. System.Console.WriteLine("----------");
  93. ConvertibleCar car2 = new ConvertibleCar();
  94. Minivan car3 = new Minivan();
  95. car2.ShowDetails();
  96. car3.ShowDetails();
  97. }
  98. // Output:
  99. // TestCars3
  100. // ----------
  101. // A roof that opens up.
  102. // Carries seven people.
  103.  
  104. public static void TestCars4()
  105. {
  106. System.Console.WriteLine("\nTestCars4");
  107. System.Console.WriteLine("----------");
  108. Car car2 = new ConvertibleCar();
  109. Car car3 = new Minivan();
  110. car2.ShowDetails();
  111. car3.ShowDetails();
  112. }
  113. // Output:
  114. // TestCars4
  115. // ----------
  116. // Standard transportation.
  117. // Carries seven people.
  118. }
  119.  
  120. // Define the base class, Car. The class defines two virtual methods,
  121. // DescribeCar and ShowDetails. DescribeCar calls ShowDetails, and each derived
  122. // class also defines a ShowDetails method. The example tests which version of
  123. // ShowDetails is used, the base class method or the derived class method.
  124. class Car
  125. {
  126. public virtual void DescribeCar()
  127. {
  128. System.Console.WriteLine("Four wheels and an engine.");
  129. ShowDetails();
  130. }
  131.  
  132. public virtual void ShowDetails()
  133. {
  134. System.Console.WriteLine("Standard transportation.");
  135. }
  136. }
  137.  
  138. // Define the derived classes.
  139.  
  140. // Class ConvertibleCar uses the new modifier to acknowledge that ShowDetails
  141. // hides the base class method.
  142. class ConvertibleCar : Car
  143. {
  144. public new void ShowDetails()
  145. {
  146. System.Console.WriteLine("A roof that opens up.");
  147. }
  148. }
  149.  
  150. // Class Minivan uses the override modifier to specify that ShowDetails
  151. // extends the base class method.
  152. class Minivan : Car
  153. {
  154. public override void ShowDetails()
  155. {
  156. System.Console.WriteLine("Carries seven people.");
  157. }
  158. }
  159.  
  160. }
Success #stdin #stdout 0.01s 131648KB
stdin
Standard input is empty
stdout
TestCars1
----------
Four wheels and an engine.
Standard transportation.
----------
Four wheels and an engine.
Standard transportation.
----------
Four wheels and an engine.
Carries seven people.
----------

TestCars2
----------
Four wheels and an engine.
Standard transportation.
----------
Four wheels and an engine.
Standard transportation.
----------
Four wheels and an engine.
Carries seven people.
----------

TestCars3
----------
A roof that opens up.
Carries seven people.

TestCars4
----------
Standard transportation.
Carries seven people.