Difference between override base class method and hide base class method the difference between override base class method and hide base class Method

Source: Internet
Author: User
The difference between the override base class method and the hidden base class method is to analyze the difference between the two, that is, the difference between the override and new keywords in the control class version. First, I will first list some official information related to this on the local MSDN 2008:

  • "Override (C # reference )":( Ms-help: // MS. MSDNQTR. v90.chs/dv_csref/html/dd1907a8-acf8-46d3-80b9-c2ca4febada8.htm)
  • "New modifier (C # reference )":( Ms-help: // MS. MSDNQTR. v90.chs/dv_csref/html/a2e20856-33b9-4620-b535-a60dbce8349b.htm)
  • "Use the Override and New keywords for version control (C # programming guide )":( Ms-help: // MS. MSDNQTR. v90.chs/dv_csref/html/88247d07-bd0d-49e9-a619-45ccbbfdf0c5.htm)
  • "Learn when to use Override and New keywords (C # programming guide )":( Ms-help: // MS. MSDNQTR. v90.chs/dv_csref/html/323db184-b136-46fc-8839-007886e7e8b0.htm)
Next, I will make a simple modification to an example in the above materials to better reflect the difference between the two in use.

Using System;

Namespace KeywordsOfOverrideAndNew
{
// Define the base class
Class Car
{
Public virtual void DescribeCar ()
{
System. Console. WriteLine ("Call the DescribeCar () defined in Car class .");
}
}

// Define the derived classes
Class ConvertibleCar: Car
{
// Use new
Public new virtual void DescribeCar ()
{
System. Console. WriteLine ("Call the DescribeCar () defined in ConvertibleCar class .");
}
}
Class Minivan: Car
{
// Use override
Public override void DescribeCar ()
{
System. Console. WriteLine ("Call the DescribeCar () defined in Minivan class .");
}
}

Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("Declare object type by each classes: \ n ");
Car car1 = new Car ();
Car1.DescribeCar ();
System. Console. WriteLine ("----------");
ConvertibleCar car2 = new ConvertibleCar ();
Car2.DescribeCar ();
System. Console. WriteLine ("----------");
Minivan car3 = new Minivan ();
Car3.DescribeCar ();
System. Console. WriteLine ("----------");

Console. WriteLine ("\ n ");

Console. WriteLine ("Declare object type by the Car class: \ n ");
Car [] cars = new Car [3];
Cars [0] = new Car ();
Cars [1] = new ConvertibleCar ();
Cars [2] = new Minivan ();

Foreach (Car vehicle in cars)
{
System. Console. WriteLine ("Car object:" + vehicle. GetType ());
Vehicle. DescribeCar ();
System. Console. WriteLine ("----------");
}

Console. Read ();
}
}
}

/*
The following lists the running results:
Declare object type by each classes:

Call the DescribeCar () defined in Car class.
----------
Call the DescribeCar () defined in ConvertibleCar class.
----------
Call the DescribeCar () defined in Minivan class.
----------

Declare object type by the Car class:

Call the DescribeCar () defined in Car class.
----------
Call the DescribeCar () defined in Car class.
----------
Call the DescribeCar () defined in Minivan class.
----------
*/

We can see from the generated results:

  • When the base class and derived class are used to declare an object, each derived class object will execute its own derived class method.
  • When a base class declares an object through polymorphism, the new method is used to hide the derived class of the base class method. Because the new keyword is used to define this method, the object created by the derived class calls not the derived class method, but the base class method. The method used to override the object created by the derived class of the base class method using override is the derived class method.
In fact, the difference between the two is to analyze the differences between the override and new keywords in the control class version. First, I will first list some official information related to this on the local MSDN 2008:

  • "Override (C # reference )":( Ms-help: // MS. MSDNQTR. v90.chs/dv_csref/html/dd1907a8-acf8-46d3-80b9-c2ca4febada8.htm)
  • "New modifier (C # reference )":( Ms-help: // MS. MSDNQTR. v90.chs/dv_csref/html/a2e20856-33b9-4620-b535-a60dbce8349b.htm)
  • "Use the Override and New keywords for version control (C # programming guide )":( Ms-help: // MS. MSDNQTR. v90.chs/dv_csref/html/88247d07-bd0d-49e9-a619-45ccbbfdf0c5.htm)
  • "Learn when to use Override and New keywords (C # programming guide )":( Ms-help: // MS. MSDNQTR. v90.chs/dv_csref/html/323db184-b136-46fc-8839-007886e7e8b0.htm)
Next, I will make a simple modification to an example in the above materials to better reflect the difference between the two in use.

Using System;

Namespace KeywordsOfOverrideAndNew
{
// Define the base class
Class Car
{
Public virtual void DescribeCar ()
{
System. Console. WriteLine ("Call the DescribeCar () defined in Car class .");
}
}

// Define the derived classes
Class ConvertibleCar: Car
{
// Use new
Public new virtual void DescribeCar ()
{
System. Console. WriteLine ("Call the DescribeCar () defined in ConvertibleCar class .");
}
}
Class Minivan: Car
{
// Use override
Public override void DescribeCar ()
{
System. Console. WriteLine ("Call the DescribeCar () defined in Minivan class .");
}
}

Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("Declare object type by each classes: \ n ");
Car car1 = new Car ();
Car1.DescribeCar ();
System. Console. WriteLine ("----------");
ConvertibleCar car2 = new ConvertibleCar ();
Car2.DescribeCar ();
System. Console. WriteLine ("----------");
Minivan car3 = new Minivan ();
Car3.DescribeCar ();
System. Console. WriteLine ("----------");

Console. WriteLine ("\ n ");

Console. WriteLine ("Declare object type by the Car class: \ n ");
Car [] cars = new Car [3];
Cars [0] = new Car ();
Cars [1] = new ConvertibleCar ();
Cars [2] = new Minivan ();

Foreach (Car vehicle in cars)
{
System. Console. WriteLine ("Car object:" + vehicle. GetType ());
Vehicle. DescribeCar ();
System. Console. WriteLine ("----------");
}

Console. Read ();
}
}
}

/*
The following lists the running results:
Declare object type by each classes:

Call the DescribeCar () defined in Car class.
----------
Call the DescribeCar () defined in ConvertibleCar class.
----------
Call the DescribeCar () defined in Minivan class.
----------

Declare object type by the Car class:

Call the DescribeCar () defined in Car class.
----------
Call the DescribeCar () defined in Car class.
----------
Call the DescribeCar () defined in Minivan class.
----------
*/

We can see from the generated results:

  • When the base class and derived class are used to declare an object, each derived class object will execute its own derived class method.
  • When a base class declares an object through polymorphism, the new method is used to hide the derived class of the base class method. Because the new keyword is used to define this method, the object created by the derived class calls not the derived class method, but the base class method. The method used to override the object created by the derived class of the base class method using override is the derived class method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.