barcode vb.net source code 11: Inheritance and Polymorphism in Visual C#

Encode Code 128C in Visual C# 11: Inheritance and Polymorphism

11: Inheritance and Polymorphism
Creating Code-128 In C#.NET
Using Barcode maker for Visual Studio .NET Control to generate, create USS Code 128 image in Visual Studio .NET applications.
www.OnBarcode.com
Scanning Code 128C In C#.NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
// an overridden version (note keyword) because in the // derived method we change the behavior public override void DrawControl( ) { base.DrawControl( ); // invoke the base method Console.WriteLine("Writing string to the ListBox: {0}", listBoxContents); } } // end ListBox
Draw Linear 1D Barcode In Visual C#.NET
Using Barcode printer for VS .NET Control to generate, create Linear image in .NET applications.
www.OnBarcode.com
UPC - 13 Generator In C#.NET
Using Barcode maker for Visual Studio .NET Control to generate, create EAN 13 image in .NET framework applications.
www.OnBarcode.com
// Button also derives from Control public class Button : Control { // constructor has no body because it simply calls // the base class constructor public Button( int top, int left) : base(top, left) { } // an overridden version (note keyword) because in the // derived method we change the behavior public override void DrawControl( ) { Console.WriteLine("Drawing a button at {0}, {1}\n", top, left); } // end Button
Generating Code 3 Of 9 In C#
Using Barcode generation for VS .NET Control to generate, create Code-39 image in VS .NET applications.
www.OnBarcode.com
UPC Symbol Creation In Visual C#
Using Barcode creator for .NET Control to generate, create GTIN - 12 image in VS .NET applications.
www.OnBarcode.com
}
2D Printer In C#.NET
Using Barcode maker for VS .NET Control to generate, create Matrix Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Identcode Encoder In Visual C#
Using Barcode generator for .NET framework Control to generate, create Identcode image in .NET framework applications.
www.OnBarcode.com
public class Tester { static void Main( ) { Control myControl = new Control(1, 2); ListBox myListBox = new ListBox(3, 4, "Standalone listbox"); Button myButton = new Button(5, 6); myControl.DrawControl( ); myListBox.DrawControl( ); myButton.DrawControl( ); Control[] controlArray = new Control[3]; controlArray[0] = new Control(1, 2); controlArray[1] = new ListBox(3, 4, "Listbox in array"); controlArray[2] = new Button(5, 6); for (int i = 0; i < controlArray.Length; i++) { controlArray[i].DrawControl( ); } // end for // end Main
Code 128C Encoder In Java
Using Barcode creation for Android Control to generate, create Code-128 image in Android applications.
www.OnBarcode.com
Generating Code128 In None
Using Barcode generation for Online Control to generate, create Code 128 image in Online applications.
www.OnBarcode.com
}
UPCA Printer In .NET Framework
Using Barcode printer for .NET framework Control to generate, create UPC-A image in Visual Studio .NET applications.
www.OnBarcode.com
Generate EAN / UCC - 13 In None
Using Barcode encoder for Online Control to generate, create EAN13 image in Online applications.
www.OnBarcode.com
Polymorphism |
Creating GS1 - 12 In Java
Using Barcode maker for Java Control to generate, create GTIN - 12 image in Java applications.
www.OnBarcode.com
Quick Response Code Generation In VS .NET
Using Barcode drawer for VS .NET Control to generate, create QR Code image in Visual Studio .NET applications.
www.OnBarcode.com
} } // end Tester
Barcode Encoder In Java
Using Barcode drawer for BIRT Control to generate, create Barcode image in BIRT applications.
www.OnBarcode.com
Barcode Creator In Java
Using Barcode drawer for BIRT Control to generate, create Barcode image in BIRT reports applications.
www.OnBarcode.com
The output looks like this:
UCC-128 Encoder In None
Using Barcode drawer for Software Control to generate, create EAN128 image in Software applications.
www.OnBarcode.com
Barcode Scanner In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
www.OnBarcode.com
Control: drawing Control at 1, 2 Control: drawing Control at 3, 4 Writing string to the ListBox: Standalone listbox Drawing a button at 5, 6 Control: drawing Control at 1, 2 Control: drawing Control at 3, 4 Writing string to the ListBox: Listbox in array Drawing a button at 5, 6
Creating 2D In Java
Using Barcode creation for Java Control to generate, create Matrix 2D Barcode image in Java applications.
www.OnBarcode.com
Painting UPC A In None
Using Barcode maker for Microsoft Excel Control to generate, create UPC A image in Microsoft Excel applications.
www.OnBarcode.com
Overriding Virtual Methods
In Example 11-2, ListBox derives from Control and implements its own version of DrawControl( ), using the override keyword:
public override void DrawWindow( ) { base.DrawWindow( ); // invoke the base method Console.WriteLine ("Writing string to the listbox: {0}", listBoxContents); }
The keyword override tells the compiler that this class has intentionally overridden how DrawControl( ) works. Similarly, you override DrawControl( ) in another class that derives from Control: the Button class. The only reason this override works is because in the base class (Control), the
DrawControl( ) method is marked as virtual:
public virtual void DrawControl( ) { Console.WriteLine("Control: drawing Control at {0}, {1}", top, left); }
If DrawControl( ) weren t marked as virtual, the derived classes wouldn t be able to override it.
Using Objects Polymorphically
The really interesting part of this example, from a polymorphic point of view, happens in the body of the example. You create three objects: a Control, a ListBox, and a Button. Then you call DrawControl( ) on each:
|
11: Inheritance and Polymorphism
Control myControl = new Control(1, 2); ListBox myListBox = new ListBox(3, 4, "Standalone listbox"); Button myButton = new Button(5, 6); myControl.DrawControl( ); myListBox.DrawControl( ); myButton.DrawControl( );
This works much as you might expect. The correct DrawControl( ) method is called for each. So far, nothing polymorphic has been done, because each of the three classes has its own version of DrawControl( ), which is what you re calling here. The real magic starts when you create an array of Control objects. As you learned in 10, an array can contain only objects of the same type. On the face of it, then, you wouldn t expect that you could store a Control, a ListBox, and a Button all in the same array. But because a ListBox is a Control, you are free to place a ListBox into an array of Controls. Similarly, you can add a Button to a collection of Controls, because a Button is a Control.
Control[] controlArray = new Control[3]; controlArray[0] = new Control(1, 2); controlArray[1] = new ListBox(3, 4, "Listbox in array"); controlArray[2] = new Button(5, 6);
The first line of the preceding code declares an array named controlArray that will hold three Control objects. The next three lines add new Control objects to the array. The first adds an object of type Control. The second adds an object of type ListBox (which is a Control because ListBox derives from Control), and the third adds an object of type Button, which is also a type of Control. What happens when you call DrawControl( ) on each of these objects
for (int i = 0; i < 3; i++) { controlArray[i].DrawControl( ); }
This code calls DrawControl( ) on each element in the array in turn. All the compiler knows is that it has three Control objects and that you ve called DrawControl( ) on each. If you had not marked DrawControl( ) as virtual, Control s original DrawControl( ) method would be called three times. However, because you did mark DrawControl( ) as virtual, and because the derived classes override that method, when you call DrawControl( ) on the array the right thing happens for each object in the array. Specifically, the compiler determines the runtime type of the actual objects (a Control, a ListBox, and a Button) and calls the right method on each. This is the essence of polymorphism that the for loop, and the code within it, have no idea what kinds of objects are going to be in the array, except that they all derive from Control, and therefore have valid DrawControl( ) methods. The for loop doesn t need to know any more than that.
Copyright © OnBarcode.com . All rights reserved.