IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesC#  Print This     

Overriding

General Info: Method Overriding

Where you define or implement a virtual method in a parent class and then replace it in a descendant class.

When you decide to declare a method as virtual, you are giving permission to derived classes to extend and override the method with their own implementation. You can have the extended method call the parent method's code too.

In most OO languages you can also choose to hide a parent method. When you introduce a new implementation of the same named method with the same signature without overriding, you are hiding the parent method.

C#:   virtual, override

In C#, you specify a virtual method with the virtual keyword in a parent class and extend (or replace) it in a descendant class using the override keyword.

Use the base keyword in the descendant method to execute the code in the parent method, i.e. base.SomeMethod().

Syntax Example:
class Robot
{
  public virtual void Speak()
  {
  }
}

class Cyborg:Robot
{
  public override void Speak()
  {
  }
}




Cross Reference Examples:

ASP Classic:   Not Supported

Since ASP Classic does not support inheritance, there is no concept of a descendant class nor overriding.

Corel Paradox:   Not Supported
Delphi:   virtual, override

In Delphi, you specify a virtual method with the virtual keyword in a parent class and extend (or replace) it in a descendant class using the override keyword. Call Inherited in the descendant method to execute the code in the parent method.

Syntax Example:
TRobot = class(TObject)
public
procedure Speak; virtual;
end;
  
TCyborg = class(TRobot)
procedure Speak; Override;
end;
Delphi Prism:   virtual, override

Same as Delphi. In Prism, you specify a virtual method with the virtual keyword in a parent class and extend (or replace) it in a descendant class using the override keyword. Call Inherited in the descendant method to execute the code in the parent method.

Use final to prevent further extending of a member and Sealed to prevent all members of a class from further extension.

Syntax Example:
Robot = class(System.Object)
public
method Speak; virtual;
end;
  
Cyborg = class(Robot)
public
method Speak; override;
end;
VB.Net:   Overridable, Overrides

In VB.Net, you specify a virtual method with the Overridable keyword in a parent class and extend (or replace) it in a descendant class using the Overrides keyword.

Use the base keyword in the descendant method to execute the code in the parent method, i.e. base.SomeMethod().

Syntax Example:
Public Class Robot
Public Overridable Sub Speak()
MessageBox.Show("Robot says hi")
End Sub
End Class
  
Public Class Cyborg
Inherits Robot
  
  Public Overrides Sub Speak()
MessageBox.Show("hi")
End Sub
End Class




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]