Difference between new and override

Source: Internet
Author: User
Abstract Class Base

{

Public Virtual VoidWork ()

{

MessageBox. Show ("Base class -- start work");

}

Public Virtual VoidOutwork ()

{

MessageBox. Show ("Base class -- off duty");

}

Public abstract void pay ();//To declare an abstract method, the quilt class new or override must be required. The abstract method can be declared only when the class is abstract.

}

Class Employee:Base

{

Public New VoidWork ()

{

MessageBox. Show ("Subclass (new) -- start to work");

}

Public Override VoidOutwork ()

{

MessageBox. Show ("Subclass (override) Off duty");

}

}

//TestCode

EmployeeEMP =New Employee();

EMP. Work ();//Subclass (new) -- start work

EMP. outwork ();//Subclass (override) Off duty

EmployeeEMP =New Employee();

BaseB = (Base) EMP;

B. Work ();//Base class -- start work

B. outwork ();//Subclass (override) Off duty

NewWhen the declared method is called using the subclass type, it runs the function in the subclass. If the type is a base class, the hidden base-class functions will come to the front-end. Only useVirtualDefine the functions in the base class and useOverrideMark the function in the subclass to achieve the desired polymorphism class (always call the subclass method ).

In subclassNewThe methods in the parent class are not necessarily virtual, that isVirtual

But in the subclassOverrideIn the parent class, the parent class method must be of the virtual type, that is

BorrowCsdnNetizen (Cforce:

Override-Overwrite (the old one does not exist)

New-New (coexistence of old and new)

Abstract classes and abstract methods

Abstract Class Animal

{

Public Abstract VoidDrink ();

Public Abstract VoidGotobed ();

}

Class Dog:Animal

{

Public Override VoidDrink ()

{

MessageBox. Show ("Dog drinking water");

}

Public Override VoidGotobed ()

{

MessageBox. Show ("Puppy sleeping");

}

Public Override StringTostring ()//You can also: Public New String tostring ()

{

Return "Puppy";

}

}

1,Abstract methods must be override. abstract methods can be declared only when the class is abstract.

2,The abstract method has no method implementation. Its subclass can only override its abstract method, but cannot be new. If it can be new, if the class type is parent class, the method of the class executes the method of the parent class instead of the method of the subclass, but the method of the parent class does not implement the method. How will the method be executed?

SelaedThe method must be used with override. That is to say, the parent class of the class that implements the sealed method must implement this method.

As follows, if a declares the virtual method A1, the sub-Class AA of A can seal and override pay. The sub-Class AA cannot overwrite or overwrite A1.

Class A

{

Public Virtual VoidA1 ()

{

MessageBox. Show (A---A1");

}

}

Class AA:A

{

Public Sealed Override VoidA1 ()

{

MessageBox. Show (AA---A1");

}

}

Summary:

Public abstract void pay (); abstractThe method has no method to implement and must be inherited.

Public sealed override void pay () {} sealedThe method must be a method to override the parent class.

Public static void pay () {} staticMethod

Public Virtual void pay () {} virtualMethod subclass canOverrideOrNew

Public new void pay (){}Parent Class MethodPayNot necessarilyVirtual

Public override void pay (){}Parent Class MethodPayIt must beVirtual

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.