61: Deriving One Class from Another in Visual C#

Printer PDF 417 in Visual C# 61: Deriving One Class from Another
Listing 61: Deriving One Class from Another
Encode PDF 417 In C#
Using Barcode drawer for .NET framework Control to generate, create PDF 417 image in Visual Studio .NET applications.
public class PdaItem { public string Name { get; set; } public string DateTimeLastUpdate { get; set; } }
Draw Bar Code In C#
Using Barcode drawer for Visual Studio .NET Control to generate, create barcode image in Visual Studio .NET applications.
Derivation
Generating PDF-417 2d Barcode In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
Figure 61: Refactoring into a Base Class
Make PDF417 In Visual Studio .NET
Using Barcode maker for Visual Studio .NET Control to generate, create PDF417 image in .NET framework applications.
// Define the Contact class as inheriting the PdaItem class public class Contact : PdaItem { public string Address { get; set; } public string Phone { get; set; } }
Draw PDF417 In VB.NET
Using Barcode drawer for VS .NET Control to generate, create PDF-417 2d barcode image in VS .NET applications.
Listing 62 shows how to access the properties defined in Contact
Bar Code Creator In Visual C#.NET
Using Barcode generation for .NET Control to generate, create barcode image in Visual Studio .NET applications.
Listing 62: Using Inherited Methods
Creating Bar Code In Visual C#.NET
Using Barcode printer for Visual Studio .NET Control to generate, create bar code image in VS .NET applications.
public class Program { public static void Main() { Contact contact = new Contact(); contactName = "Inigo Montoya"; // } }
Making Code 39 In Visual C#.NET
Using Barcode printer for .NET framework Control to generate, create Code-39 image in Visual Studio .NET applications.
6: Inheritance
Code 128 Code Set C Generator In Visual C#.NET
Using Barcode maker for .NET Control to generate, create Code 128 Code Set C image in VS .NET applications.
Even though Contact does not directly have a property called Name, all instances of Contact can still access the Name property from PdaItem and use it as though it was part of Contact Furthermore, any additional classes that derive from Contact will also inherit the members of PdaItem, or any class from which PdaItem was derived The inheritance chain has no practical limit and each derived class will have all the exposed members of its base class inheritance chain combined (see Listing 63)
Paint Data Matrix In C#
Using Barcode generator for .NET framework Control to generate, create Data Matrix 2d barcode image in .NET applications.
Listing 63: Classes Deriving from Each Other to Form an Inheritance Chain
UCC - 12 Generation In Visual Studio .NET
Using Barcode generator for ASP.NET Control to generate, create UPC-A Supplement 2 image in ASP.NET applications.
public class PdaItem : object { // }
Encoding Bar Code In Visual Basic .NET
Using Barcode creator for VS .NET Control to generate, create barcode image in .NET applications.
public class Appointment : PdaItem { // }
Draw Bar Code In Visual Studio .NET
Using Barcode generator for ASP.NET Control to generate, create bar code image in ASP.NET applications.
public class Contact : PdaItem { // }
UCC-128 Encoder In VS .NET
Using Barcode generation for ASP.NET Control to generate, create USS-128 image in ASP.NET applications.
public class Customer : Contact { // }
Encoding Bar Code In Visual Studio .NET
Using Barcode generator for .NET framework Control to generate, create barcode image in Visual Studio .NET applications.
In other words, although Customer doesn t derive from PdaItem directly, it still inherits the members of PdaItem In Listing 63, PdaItem is shown explicitly to derive from object Although C# allows such syntax, it is unnecessary because all objects that don t have some other derivation will derive from object, regardless of whether it is specified
DataMatrix Creation In .NET Framework
Using Barcode generator for ASP.NET Control to generate, create Data Matrix 2d barcode image in ASP.NET applications.
Casting between Base and Derived Types As Listing 64 shows, because derivation forms an is a relationship, a derived type can always be directly assigned to a base type
Paint Code-128 In .NET
Using Barcode maker for ASP.NET Control to generate, create Code 128 Code Set C image in ASP.NET applications.
Derivation
Making Bar Code In Java
Using Barcode creation for Java Control to generate, create bar code image in Java applications.
Listing 64: Implicit Base Type Casting
Bar Code Drawer In VB.NET
Using Barcode creation for .NET framework Control to generate, create barcode image in VS .NET applications.
public class Program { pulic static void Main() { // Derived types can be cast implicitly to // base types Contact contact = new Contact(); PdaItem item = contact; // // Base types must be cast explicitly to derived types contact = (Contact)item; // } }
Printing EAN13 In Java
Using Barcode creator for Java Control to generate, create EAN13 image in Java applications.
The derived type, Contact, is a PdaItem and can be assigned directly to a PdaItem This is known as an implicit cast because no specific operator is required and the conversion will, on principle, always succeed; it will not throw an exception
BEGINNER TOPIC Casting within the Inheritance Chain The cast to a base class does not instantiate a new instance Instead, the same instance is simply referred to as the base type and the capabilities (the accessible members) are those of the base type It is just like referring to a CD as a storage device Since not all storage devices support an eject operation, a CD that is cast to a storage device cannot be ejected either, and a call to storageDeviceEject() would not compile even though the instantiated object may have been a CD object that supported the Eject() method Similarly, casting down from the base class to the derived cast simply begins referring to the type more specifically, expanding the available operations The restriction is that the actual instantiated type must be an instance of the targeted type (or something derived from it)
The reverse, however, is not true A PdaItem is not necessarily a Contact; it could be an Appointment or some other undefined, class-derived
6: Inheritance
type Therefore, casting from the base type to the derived type requires an explicit cast, which at runtime could fail To perform an explicit cast, identify the target type within parentheses prior to the original reference, as Listing 64 demonstrates With the explicit cast, the programmer essentially communicates to the compiler to trust her, she knows what she is doing, and the C# compiler allows the conversion as long as the target type is derived from the originating type Although the C# compiler allows an explicit conversion at compile time between potentially compatible types, the CLR will still verify the explicit cast at execution time, throwing an exception if in fact the base type is not of the targeted type The C# compiler allows the cast operator even when the type hierarchy allows an implicit cast For example, the assignment from contact to item could use a cast operator as follows:
item = (PdaItem)contact;
or even when no cast is necessary:
contact = (Contact)contact;
ADVANCED TOPIC Defining Custom Conversions Casting between types is not limited to types within a single inheritance chain It is possible to cast entirely unrelated types as well The key is the provision of a conversion operator between the two types C# allows types to include either explicit or implicit cast operators Anytime the operation could possibly fail, such as in a cast from long to int, developers should choose to define an explicit cast operator This warns developers performing the cast to do so only when they are certain the cast will succeed, or else to be prepared to catch the exception if it doesn t They should also use explicit casts over an implicit cast when the conversion is lossy Converting from a float to an int, for example, truncates the decimal, which a return cast (from int back to float) would not recover Listing 65 shows implicit and explicit cast operators for Address to string and vice versa