Working with Inheritance in VS .NET

Drawing PDF417 in VS .NET Working with Inheritance

12
PDF 417 Drawer In .NET
Using Barcode encoder for ASP.NET Control to generate, create PDF417 image in ASP.NET applications.
www.OnBarcode.com
Bar Code Creation In .NET
Using Barcode creator for ASP.NET Control to generate, create bar code image in ASP.NET applications.
www.OnBarcode.com
} ... } ... }
PDF417 Generation In C#.NET
Using Barcode generation for .NET Control to generate, create PDF-417 2d barcode image in .NET framework applications.
www.OnBarcode.com
PDF-417 2d Barcode Printer In .NET
Using Barcode encoder for VS .NET Control to generate, create PDF-417 2d barcode image in VS .NET applications.
www.OnBarcode.com
Working with Inheritance
PDF 417 Maker In VB.NET
Using Barcode creator for VS .NET Control to generate, create PDF417 image in Visual Studio .NET applications.
www.OnBarcode.com
Barcode Generation In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create barcode image in ASP.NET applications.
www.OnBarcode.com
Note Java developers should note that C# methods are not virtual by default.
Painting Barcode In .NET Framework
Using Barcode generator for ASP.NET Control to generate, create barcode image in ASP.NET applications.
www.OnBarcode.com
Data Matrix 2d Barcode Creation In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create ECC200 image in ASP.NET applications.
www.OnBarcode.com
Declaring override Methods
Print Barcode In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create barcode image in ASP.NET applications.
www.OnBarcode.com
Print QR In .NET
Using Barcode printer for ASP.NET Control to generate, create QR Code image in ASP.NET applications.
www.OnBarcode.com
If a base class declares that a method is virtual, a derived class can use the override keyword to declare another implementation of that method. For example:
PDF 417 Printer In .NET
Using Barcode creation for ASP.NET Control to generate, create PDF417 image in ASP.NET applications.
www.OnBarcode.com
Rationalized Codabar Creation In .NET Framework
Using Barcode creation for ASP.NET Control to generate, create Ames code image in ASP.NET applications.
www.OnBarcode.com
class Horse : Mammal { ... public override string ToString() { ... } }
Data Matrix 2d Barcode Reader In C#.NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Creating Code 39 Full ASCII In None
Using Barcode maker for Online Control to generate, create Code 3/9 image in Online applications.
www.OnBarcode.com
The new implementation of the method in the derived class can call the original implementation of the method in the base class by using the base keyword, like this:
European Article Number 13 Maker In None
Using Barcode drawer for Excel Control to generate, create GS1 - 13 image in Office Excel applications.
www.OnBarcode.com
Make UPC-A Supplement 5 In Objective-C
Using Barcode creator for iPhone Control to generate, create UPC Symbol image in iPhone applications.
www.OnBarcode.com
public override string ToString() { base.ToString(); ... }
Encode PDF 417 In None
Using Barcode maker for Microsoft Excel Control to generate, create PDF 417 image in Office Excel applications.
www.OnBarcode.com
Recognize Code 39 Extended In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
There are some important rules you must follow when declaring polymorphic methods (see the following sidebar, Virtual Methods and Polymorphism ) by using the virtual and override keywords: You re not allowed to declare a private method when using the virtual or override keyword. If you try, you ll get a compile-time error. Private really is private. The two method signatures must be identical that is, they must have the same name, the same number and type of parameters, and the same return type. The two methods must have the same access. For example, if one of the two methods is public, the other must also be public. (Methods can also be protected, as you will nd out in the next section.)
Recognize Barcode In .NET Framework
Using Barcode Control SDK for ASP.NET Control to generate, create, read, scan barcode image in ASP.NET applications.
www.OnBarcode.com
UPC Symbol Generation In C#
Using Barcode maker for VS .NET Control to generate, create UPCA image in .NET framework applications.
www.OnBarcode.com
Part II
Understanding the C# Language
You can override only a virtual method. If the base class method is not virtual and you try to override it, you ll get a compile-time error. This is sensible; it should be up to the designer of the base class to decide whether its methods can be overridden. If the derived class does not declare the method by using the override keyword, it does not override the base class method. In other words, it becomes an implementation of a completely different method that happens to have the same name. As before, this will cause a compile-time hiding warning, which you can silence by using the new keyword as previously described. An override method is implicitly virtual and can itself be overridden in a further derived class. However, you are not allowed to explicitly declare that an override method is virtual by using the virtual keyword.
Virtual Methods and Polymorphism
Virtual methods enable you to call different versions of the same method, based on the type of the object determined dynamically at run time. Consider the following example classes that de ne a variation on the Mammal hierarchy described earlier:
class Mammal { ... public virtual string GetTypeName() { return This is a mammal ; } } class Horse : Mammal { ... public override string GetTypeName() { return This is a horse ; } } class Whale : Mammal { ... public override string GetTypeName () { return This is a whale ; } } class Aardvark : Mammal { ... }
12
Working with Inheritance
Notice two things: rst, the override keyword used by the GetTypeName method (which will be described shortly) in the Horse and Whale classes, and second, the fact that the Aardvark class does not have a GetTypeName method. Now examine the following block of code:
Mammal myMammal; Horse myHorse = new Horse(...); Whale myWhale = new Whale(...); Aardvark myAardvark = new Aardvark(...); myMammal = myHorse; Console.WriteLine(myMammal.GetTypeName()); // Horse myMammal = myWhale; Console.WriteLine(myMammal.GetTypeName()); // Whale myMammal = myAardvark; Console.WriteLine(myMammal.GetTypeName()); // Aardvark
What will be output by the three different Console.WriteLine statements At rst glance, you would expect them all to print This is a mammal, because each statement calls the GetTypeName method on the myMammal variable, which is a Mammal. However, in the rst case, you can see that myMammal is actually a reference to a Horse. (Remember, you are allowed to assign a Horse to a Mammal variable because the Horse class is derived from the Mammal class.) Because the GetTypeName method is de ned as virtual, the runtime works out that it should call the Horse.GetTypeName method, so the statement actually prints the message This is a horse. The same logic applies to the second Console.WriteLine statement, which outputs the message This is a whale. The third statement calls Console.WriteLine on an Aardvark object. However, the Aardvark class does not have a GetTypeName method, so the default method in the Mammal class is called, returning the string This is a mammal. This phenomenon of the same statement invoking a different method is called polymorphism, which literally means many forms.
Copyright © OnBarcode.com . All rights reserved.