My Query....
In Test1 method when we are calling the car2.DescribeCar() method ..
why it is calling the Base Class("CAR") method ShowDetails() instead of
derived class("CovertiableCar") method of ShowDetails()..
Because derived Class object always call the derived methods first then only Calls the base class methods...
////////Code...................................................
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OverrideAndNew2 { class Program { static void Main(string[] args) { // Declare objects of the derived classes and test which version // of ShowDetails is run, base or derived. TestCars1(); // Declare objects of the base class, instantiated with the // derived classes, and repeat the tests. TestCars2(); // Declare objects of the derived classes and call ShowDetails // directly. TestCars3(); // Declare objects of the base class, instantiated with the // derived classes, and repeat the tests. TestCars4(); } public static void TestCars1() { System.Console.WriteLine("\nTestCars1"); System.Console.WriteLine("----------"); Car car1 = new Car(); car1.DescribeCar(); System.Console.WriteLine("----------"); // Notice the output from this test case. The new modifier is // used in the definition of ShowDetails in the ConvertibleCar // class. ConvertibleCar car2 = new ConvertibleCar(); car2.DescribeCar(); System.Console.WriteLine("----------"); Minivan car3 = new Minivan(); car3.DescribeCar(); System.Console.WriteLine("----------"); } // Output: // TestCars1 // ---------- // Four wheels and an engine. // Standard transportation. // ---------- // Four wheels and an engine. // Standard transportation. // ---------- // Four wheels and an engine. // Carries seven people. // ---------- public static void TestCars2() { System.Console.WriteLine("\nTestCars2"); System.Console.WriteLine("----------"); var cars = new List{ new Car(), new ConvertibleCar(), new Minivan() }; foreach (var car in cars) { car.DescribeCar(); System.Console.WriteLine("----------"); } } // Output: // TestCars2 // ---------- // Four wheels and an engine. // Standard transportation. // ---------- // Four wheels and an engine. // Standard transportation. // ---------- // Four wheels and an engine. // Carries seven people. // ---------- public static void TestCars3() { System.Console.WriteLine("\nTestCars3"); System.Console.WriteLine("----------"); ConvertibleCar car2 = new ConvertibleCar(); Minivan car3 = new Minivan(); car2.ShowDetails(); car3.ShowDetails(); } // Output: // TestCars3 // ---------- // A roof that opens up. // Carries seven people. public static void TestCars4() { System.Console.WriteLine("\nTestCars4"); System.Console.WriteLine("----------"); Car car2 = new ConvertibleCar(); Car car3 = new Minivan(); car2.ShowDetails(); car3.ShowDetails(); } // Output: // TestCars4 // ---------- // Standard transportation. // Carries seven people. } // Define the base class, Car. The class defines two virtual methods, // DescribeCar and ShowDetails. DescribeCar calls ShowDetails, and each derived // class also defines a ShowDetails method. The example tests which version of // ShowDetails is used, the base class method or the derived class method. class Car { public virtual void DescribeCar() { System.Console.WriteLine("Four wheels and an engine."); ShowDetails(); } public virtual void ShowDetails() { System.Console.WriteLine("Standard transportation."); } } // Define the derived classes. // Class ConvertibleCar uses the new modifier to acknowledge that ShowDetails // hides the base class method. class ConvertibleCar : Car { public new void ShowDetails() { System.Console.WriteLine("A roof that opens up."); } } // Class Minivan uses the override modifier to specify that ShowDetails // extends the base class method. class Minivan : Car { public override void ShowDetails() { System.Console.WriteLine("Carries seven people."); } } }
Loading

Jignesh TrivediPosted Sep 11, 2014, 12:04 AM
hi,
The override modifier extends the base class method, and the new modifier hides it.
if you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it.
also refer my article
http://www.c-sharpcorner.com/UploadFile/ff2f08/overriding-vs-shadowing-in-C-Sharp/
hope this will help you.
Abhishek JaiswalPosted Sep 10, 2014, 2:52 PM
check this
http://www.c-sharpcorner.com/UploadFile/2072a9/virtual-vs-override-vs-new-keywords-in-csharp/?#ReadAndPostComment
:)
VulpesPosted Sep 10, 2014, 9:48 AM
However, it is not overridden in the derived class ConvertibleCar.
Consequently, when the run time sees this code:
ConvertibleCar car2 = new ConvertibleCar();
car2.DescribeCar();
it looks up the inheritance chain until it finds the first ancestor class which provides an implementation of the DescribeCar() method.
This is Car's own implementation and so it calls that. This method in turn calls Car's implementation of the virtual ShowDetails() method because it isn't overridden in the ConvertibleCar class, it's merely hidden.