Differences between the new modifier and the override Modifier

Source: Internet
Author: User

I replied to a post from a netizen a few days ago about the difference between the new modifier and the override modifier. I found myself vague at first glance, the description on msdn cannot even completely clarify the difference, but it is not a serious problem in use. Simple Description:
Virtual: Virtual method. You can inherit from the parent class or subclass to implement your own personality, that is, polymorphism among the three features of OO. It is determined by the runtime to call that function.
New: hides the virtual method of the parent class. By default, the method implemented by the parent class is called, but the specified method called by the parent class is displayed. Default behavior of a function with the same name.
Override: overwrites the method of the parent class and can only call its own method.
Abstract: Abstract METHODS (or classes) do not contain implementations. Non-Abstract subclasses must override them. You can specify a method as abstract to force subclass to implement it.
Overload: Call the method of the same name through the parameter list. This should be determined during compilation.

Code
Code
Public   Class Baseclass
{
Public   Virtual   Void Func1 ()
{
Console. writeline ( " Base. F1 " );
}
}

Public ClassChildclass1: baseclass
{
Public Override VoidFunc1 ()
{
Console. writeline ("(Overrided) c1.myf1");
}
}

Public   Class Childclass2: baseclass
{
Public   New   Void Func1 ()
{
Console. writeline ( " (New) c2.newf1 " );
}
}
Static   Void Main ( String [] ARGs)
{
BaseclassBa_c1 =   New Childclass1 ();
BaseclassBa_c2 =   New Childclass2 ();
Childclass1C1_c1 = (Childclass1) ba_c1;
Childclass2C2_c2 = (Childclass2) ba_c2;
// Override
Console. Write ( " Ba_c1: " );
Ba_c1.func1 ();
Console. Write ( " C1_c1: " );
C1_c1.func1 ();
// New
Console. Write ( " Ba_c2: " );
Ba_c2.func1 ();
Console. Write ( " C2_c2: " );
C2_c2.func1 ();
}

For example Code , Override, can only output its own method, while new, you can use the parent class type to call its parent method. The result of the above Code is:
---------------------------------------------------------------
Ba_c1: (overrided)C1.myf1 // Override: the parent class method has been overwritten.
C1_c1: (overrided) c1.myf1
Ba_c2:Base. F1 // New: displays the call to the parent class method.
C2_c2: (new) c2.newf1
--------------------------------------------------------------------------

For example, in a vector graphics display module, various types of MapObject are defined, that is, data is organized using mapobjectcollection. This is when the draw method of each element in the set is called, then, each element should override the draw method of the parent class to achieve correct painting.
------------------------------
On the other hand, there was a colleague who once gave me the interface (where the interface is occupied in memory), and the interface was just a Protocol (the compiler can understand it ), how can I have an instance? Therefore, the interface cannot have constructor. If so, it is a specific object and a specific class. It is like specifying the attributes and actions that some objects should have, and how the protocol uses data, and how can they occupy a location in the memory without data? Specifying an interface to declare an object is equivalent to determining which method or member variable you want to call this object. I have read a post about interfaces for a long time before, And I have referenced Jackie Chan's film ....., what an object is depends on how you construct it using the constructor of that class. It does not matter what type of declaration or pointer you use. For example, a handle is also a pointer to the actual structure, but the operating system converts it to a pointer pointing to a null structure to encapsulate data. The relationship between an object and its parent class is "is ". The relationship with the interface is "satisfied" or "satisfied". You can convert the pointer type to another type, but what it is depends on the constructor next to new.

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.