ASP Classic:
Not Supported
Since ASP Classic does not support inheritance, there is no concept of a descendant class nor overriding.
|
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() { } }
|
Override Details
- You cannot override a regular non-virtual method, nor a static method.
- The first version of the parent method must be virtual or abstract.
- You can override any parent method marked virtual, abstract, or override (already overridden).
- The methods must have the same signature.
- The methods must have the same visibility (the same access level).
- Use the base keyword to refer to the parent class as in base.SomeMethod().
C# Override Example
The following code snippet demonstrates using virtual and override to override a parent method in a descendant class. using System; class Dog
{ public virtual void Bark() { Console.WriteLine("RUFF!"); } }
class GermanShepard:Dog { public override void Bark() { Console.WriteLine("Rrrrooouuff!!"); } }
class Chiuaua:Dog { public override void Bark() { Console.WriteLine("ruff"); } }
class InclusionExample { public static void Main() { Dog MyDog=new Dog();
MyDog=new GermanShepard(); MyDog.Bark(); // prints Rrrrooouuff!!
MyDog=new Chiuaua();
MyDog.Bark(); // prints ruff; } }
Hiding a Method with New
Use the new keyword to introduce a new implementation of a parent method (this hides the parent method). You can hide a method without using new but you will get a compiler warning. Using new will suppress the warning.
The new and override modifiers have different meanings. The new modifier creates a new member with the same name, signature, and visibility and hides the original member. The override modifier extends the implementation for an inherited member and allows you to implement inheritance-based polymorphism.
Avoid Introducing New Members: Sometimes there are clear reasons to introduce a new method with the same name, signature, and visibility of a parent method. In those clear cases, introducing a new member is a powerful feature. However, if you do not have a clear reason, then avoid introducing a new version of a method by naming the new method something unique and appropriate. class Robot : System.Object { public void Speak() { MessageBox.Show("Robot says hi"); } } class Cyborg : Robot { new public void Speak() { MessageBox.Show("hi"); } }
Calling the Base Class Version
A common task In OO is to extend a method by first executing the parent method code and then adding code. Use the base keyword to refer to the parent class as in base.SomeMethod(). class Robot : System.Object { public virtual void Speak() { MessageBox.Show("Robot says hi"); } } class Cyborg : Robot { public override void Speak() { base.Speak(); MessageBox.Show("hi"); } }
More Info
|
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;
|
Working Delphi 2009 Override Example
The following code assumes a Windows application with a single form�with a button. It�demonstrates using virtual and override to override a parent method in a descendant class. unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); end; TRobot = class(TObject) public procedure Speak; virtual; end; TCyborg = class(TRobot) procedure Speak; override; end; var Form1: TForm1; implementation {$R *.dfm} procedure TRobot.Speak; begin ShowMessage('Robot says hi'); end; procedure TCyborg.Speak; begin ShowMessage('Cyborg says hi'); end;
procedure TForm1.Button1Click(Sender: TObject); var Robot: TRobot; Cyborg: TCyborg; begin Robot := TRobot.Create; Cyborg := TCyborg.Create; Robot.Speak; Cyborg.Speak; FreeAndNil(Robot); FreeAndNil(Cyborg); end; end.
Hiding a Method with 'reintroduce'
Use the reintroduce keyword to introduce a new implementation of a parent method (this hides the parent method). You can hide a method without using�reintroduce but you will get a compiler warning. Using�reintroduce will suppress the warning.
The�reintroduce and override modifiers have different meanings. The�reintroduce modifier creates a new member with the same name, signature, and visibility and hides the original member. The override modifier extends the implementation for an inherited member and allows you to implement inheritance-based polymorphism.
Avoid Introducing New Members: Sometimes there are clear reasons to introduce a new method with the same name, signature, and visibility of a parent method. In those clear cases, introducing a new member is a powerful feature. However, if you do not have a clear reason, then avoid introducing a new version of a method by naming the new method something unique and appropriate. //interface section: TRobot = class(TObject) public procedure Speak; end; TCyborg = class(TRobot) procedure Speak; reintroduce; end; //implementation section: procedure TRobot.Speak; begin ShowMessage('Robot says hi'); end; procedure TCyborg.Speak; begin
ShowMessage('Cyborg says hi'); end;
Invoking the Parent Method with 'inherited'
A common task In OO is to extend a method by first executing the parent method code and then adding code. Use the Inherited keyword to invoke the parent class method. method Robot.Speak; begin MessageBox.Show("Robot says hi"); end; method Cyborg.Speak; begin inherited; MessageBox.Show("Cyborg says hi"); end;
More Info
|
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;
|
Override Details
- You cannot override a regular non-virtual method, nor a static method.
- The first version of the parent method must be virtual or abstract.
- You can override any parent method marked virtual, abstract, or override (already overridden).
- The methods must have the same signature.
- The methods must have the same visibility (the same access level).
- Use the Inherited keyword to refer to cal the parent class method.
Working Prism Override Example
The following code assumes a Windows application with a single form with a button. It demonstrates using virtual and override to override a parent method in a descendant class. namespace CR_Override; interface uses System.Drawing, System.Collections, System.Collections.Generic, System.Linq, System.Windows.Forms, System.ComponentModel; type /// <summary> /// Summary description for MainForm. /// </summary> MainForm = partial class(System.Windows.Forms.Form) private method button1_Click(sender: System.Object; e: System.EventArgs); protected method Dispose(disposing: Boolean); override; public constructor; end; Robot = class(System.Object) public method Speak; virtual; end; Cyborg = class(Robot) public method Speak; override; end; implementation {$REGION Construction and Disposition} constructor MainForm; begin InitializeComponent(); end; method MainForm.Dispose(disposing: Boolean); begin if disposing then begin if assigned(components) then components.Dispose(); end; inherited Dispose(disposing); end; {$ENDREGION} method Robot.Speak; begin MessageBox.Show("Robot says hi"); end;
method Cyborg.Speak; begin MessageBox.Show("Cyborg says hi"); end; method MainForm.button1_Click(sender: System.Object; e: System.EventArgs); begin var MyRobot: Robot; var MyCyborg: Cyborg; MyRobot := new Robot; MyCyborg := new Cyborg; MyRobot.Speak; MyCyborg.Speak; end; end.
Hiding a Method with 'reintroduce'
Use the reintroduce keyword to introduce a new implementation of a parent method (this hides the parent method). You can hide a method without using reintroduce but you will get a compiler warning. Using reintroduce will suppress the warning.
The reintroduce and override modifiers have different meanings. The reintroduce modifier creates a new member with the same name, signature, and visibility and hides the original member. The override modifier extends the implementation for an inherited member and allows you to implement inheritance-based polymorphism.
Avoid Introducing New Members: Sometimes there are clear reasons to introduce a new method with the same name, signature, and visibility of a parent method. In those clear cases, introducing a new member is a powerful feature. However, if you do not have a clear reason, then avoid introducing a new version of a method by naming the new method something unique and appropriate. //interface section: Robot = class(System.Object) public method Speak; end; Cyborg = class(Robot) public method Speak; reintroduce; end; //implementation section: method Robot.Speak; begin MessageBox.Show("Robot says hi"); end; method Cyborg.Speak; begin MessageBox.Show("Cyborg says hi"); end;
Calling the Base Class Version
A common task In OO is to extend a method by first executing the parent method code and then adding code. Use the Inherited keyword to invoke the parent class method. method Robot.Speak; begin MessageBox.Show("Robot says hi"); end; method Cyborg.Speak; begin inherited; MessageBox.Show("Cyborg says hi"); end;
More Info
|
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
|
Override Details
- You cannot override a regular non-virtual method, nor a static method.
- The first version of the parent method must be Overridable or MustOverride.
- You can override any parent method marked Overridable, MustOverride, or Overrides (already overridden).
- The methods must have the same signature.
- The methods must have the same visibility (the same access level).
- Use the base keyword to refer to the parent class as in MyBase.SomeMethod().
Working VB.Net 2008 Override Example
The following code assumes a Windows application with a single form with a button. It demonstrates using Overridable and Overrides to override a parent method in a descendant class. Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim MyRobot As Robot Dim MyCyborg As Cyborg MyRobot = New Robot MyCyborg = New Cyborg MyRobot.Speak() MyCyborg.Speak() End Sub End Class 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
Hiding a Method with Shadows
Use the Shadows keyword to introduce a new implementation of a parent method (this hides the parent method). You can hide a method without using Shadows but you will get a compiler warning. Using Shadows will suppress the warning.
The Shodaws and Overrides modifiers have different meanings. The Shadows modifier creates a new member with the same name, signature, and visibility and hides the original member. The Overrides modifier extends the implementation for an inherited member and allows you to implement inheritance-based polymorphism.
Avoid Introducing New Members: Sometimes there are clear reasons to use Shadows to introduce a new method with with the same name, signature, and visibility of a parent method. In those clear cases, introducing a new member is a powerful feature. However, if you do not have a clear reason, then avoid introducing a new version of a method by naming the new method something unique and appropriate. Public Class Robot Public Sub Speak() MessageBox.Show("Robot says hi") End Sub End Class Public Class Cyborg Inherits Robot Public Shadows Sub Speak() MessageBox.Show("hi") End Sub End Class
Calling the Parent Class Version with MyBase
A common task In OO is to extend a method by first executing the parent method code and then adding code. Use the MyBase keyword to refer to the parent class as in MyBase.SomeMethod(). 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() MyBase.Speak() MessageBox.Show("hi") End Sub End Class
More Info
|