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

Painting ANSI/AIM Code 128 in Visual C#.NET 11: Inheritance and Polymorphism

11: Inheritance and Polymorphism
USS Code 128 Creator In Visual C#.NET
Using Barcode creator for .NET Control to generate, create Code 128C image in VS .NET applications.
www.OnBarcode.com
Code 128C Recognizer In C#.NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
while, you realize that they share certain characteristics and behaviors that are more specialized than Control, but more general than any of the three. You might factor these common traits and behaviors into a common base class, Button, and rearrange your inheritance hierarchy, as shown in Figure 11-3. This is how you d use generalization in object-oriented development.
UPC Code Encoder In Visual C#
Using Barcode generator for .NET Control to generate, create Universal Product Code version A image in VS .NET applications.
www.OnBarcode.com
Matrix Barcode Creation In C#
Using Barcode printer for .NET Control to generate, create 2D Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Control
Printing EAN 128 In Visual C#
Using Barcode maker for .NET framework Control to generate, create GS1-128 image in .NET framework applications.
www.OnBarcode.com
Generating Data Matrix ECC200 In C#
Using Barcode generation for .NET Control to generate, create DataMatrix image in .NET applications.
www.OnBarcode.com
Button
Barcode Generator In C#
Using Barcode creator for .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
Paint GTIN - 12 In C#.NET
Using Barcode printer for .NET Control to generate, create UPC-E image in VS .NET applications.
www.OnBarcode.com
ListBox
Create ANSI/AIM Code 128 In Java
Using Barcode generator for Java Control to generate, create Code 128 Code Set B image in Java applications.
www.OnBarcode.com
Code 128 Code Set B Creation In .NET Framework
Using Barcode generator for Reporting Service Control to generate, create Code 128 Code Set C image in Reporting Service applications.
www.OnBarcode.com
Control
Code 3/9 Creation In Objective-C
Using Barcode drawer for iPhone Control to generate, create Code 3 of 9 image in iPhone applications.
www.OnBarcode.com
Paint European Article Number 13 In Objective-C
Using Barcode generation for iPhone Control to generate, create EAN-13 Supplement 5 image in iPhone applications.
www.OnBarcode.com
RadioButton
ANSI/AIM Code 128 Reader In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
QR Code JIS X 0510 Generation In Java
Using Barcode encoder for Java Control to generate, create QR-Code image in Java applications.
www.OnBarcode.com
CheckBox
Code39 Encoder In None
Using Barcode encoder for Software Control to generate, create Code 39 Full ASCII image in Software applications.
www.OnBarcode.com
Code 128 Code Set B Generation In Objective-C
Using Barcode creator for iPhone Control to generate, create Code 128C image in iPhone applications.
www.OnBarcode.com
Command
DataMatrix Scanner In C#
Using Barcode recognizer for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
UPCA Creator In None
Using Barcode maker for Software Control to generate, create GS1 - 12 image in Software applications.
www.OnBarcode.com
ListBox
Making GS1 - 12 In .NET
Using Barcode encoder for Reporting Service Control to generate, create UPC-A Supplement 2 image in Reporting Service applications.
www.OnBarcode.com
PDF-417 2d Barcode Drawer In Java
Using Barcode creator for Android Control to generate, create PDF 417 image in Android applications.
www.OnBarcode.com
Control
Button
ListBox
CheckBox
Command
RadioButton
Specialization and Generalization |
The UML diagram in Figure 11-3 depicts the relationship among the factored classes and shows that both ListBox and Button derive from Control, and that Button is specialized into CheckBox and Command. Finally, RadioButton derives from CheckBox. You can thus say that RadioButton is a CheckBox, which in turn is a Button, and that Buttons are Controls. This is not the only, or even necessarily the best, organization for these objects. Whenever you design your own classes, you ll probably come up with several different ways to organize them. It s a reasonable starting point for understanding how these types (classes) relate to one another, though.
Inheritance
Now that you have the background of specialization down, and a starting-point example to work with, you can see how to use this idea in your code. In C#, the specialization relationship is implemented using a principle called inheritance. This is not the only way to implement specialization, but it is the most common and most natural way. Saying that ListBox inherits from (or derives from) Control indicates that it specializes Control. Control is referred to as the base class, and ListBox is referred to as the derived class. That is, ListBox derives its characteristics and behaviors from Control and then specializes to its own particular needs.
You ll often see the immediate base class referred to as the parent class and the derived class referred to as the child class, whereas the topmost class, Object, is called the root class.
Implementing Inheritance
In C#, you create a derived class by adding a colon after the name of the derived class, followed by the name of the base class:
public class ListBox : Control
This code declares a new class, ListBox, which derives from Control. You can read the colon as derives from. The derived class inherits all the members of the base class (both member variables and methods). In other words, suppose Control has member fields called top and left, to indicate where on the screen the upper-left corner of the Control will be drawn. If ListBox derives from Control, ListBox also has the member fields top and left. The same is true of methods: if Control has a method called DrawControl( ), ListBox does too. Methods of the derived class have access to all the public and protected members of the base class. That means that if the drawControl( ) method in Control is marked as
|
11: Inheritance and Polymorphism
protected, the ListBox class can call that method, whereas a class that doesn t derive from Control wouldn t be able to.
The derived class is free to implement its own version of a base class method that is, ListBox can have its own drawControl( ) method. This is called hiding the base class method and is accomplished by marking the method with the keyword new. (Many C# programmers advise never hiding base class methods as it is unreliable, hard to maintain, and confusing.) The new keyword indicates that the derived class has intentionally hidden and replaced the base class method. (We also discuss the new keyword in Versioning with new and override later in this chapter.)
This is a different use of the keyword new than you saw earlier in this book. In 7, we used new to create an object on the heap; here, we re using new to replace the base class method. Programmers say the keyword new is overloaded, which means that the word has more than one meaning or use.
Example 11-1 shows the ListBox class inheriting from Control, and demonstrates all the features we just talked about. Note that although Control and ListBox are the names of legitimate Windows classes, that s not what we re showing you here. These are custom classes with familiar names to help you understand the inheritance relationship.
using using using using System; System.Collections.Generic; System.Linq; System.Text;
namespace Example_11_1_ _ _ _Inheritance { public class Control { // these members are private and thus invisible // to derived class methods private int top; private int left; // constructor takes two integers to // fix location on the console public Control(int top, int left) { this.top = top; this.left = left; } // simulates drawing the control public void DrawControl( ) {
Copyright © OnBarcode.com . All rights reserved.