free barcode generator in asp.net c# CLASSES AND INHERITANCE in Visual C#

Encoder ECC200 in Visual C# CLASSES AND INHERITANCE

CHAPTER 7 CLASSES AND INHERITANCE
Data Matrix 2d Barcode Encoder In C#
Using Barcode maker for .NET framework Control to generate, create ECC200 image in .NET applications.
www.OnBarcode.com
Read ECC200 In Visual C#
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
All Classes are Derived From Class object
Encode 1D In C#.NET
Using Barcode printer for .NET framework Control to generate, create Linear 1D Barcode image in .NET framework applications.
www.OnBarcode.com
PDF-417 2d Barcode Generator In Visual C#.NET
Using Barcode generation for VS .NET Control to generate, create PDF-417 2d barcode image in .NET framework applications.
www.OnBarcode.com
All classes, except special class object, are derived classes, even if they don t have a class-base specification. Class object is the only one that is not derived, since it is the base of the inheritance hierarchy. Classes without a class-base specification are implicitly derived directly from class object. Leaving off the class-base specification is just shorthand for specifying that object is the base class. The two forms are completely equivalent. Figure 7-2 shows both forms of declaration for the same class.
Generate Barcode In Visual C#.NET
Using Barcode printer for .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
UPC-A Supplement 2 Maker In C#
Using Barcode generation for Visual Studio .NET Control to generate, create UPCA image in .NET framework applications.
www.OnBarcode.com
Figure 7-2. Direct inheritance from object Other important facts about class derivation are the following: A class declaration can have only a single class listed in its class-base specification. This is called single inheritance. Although a class can directly inherit from only a single base class, there is no limit to the level of derivation. That is, the class listed as the base class might be derived from another class, which is derived from another class, and so forth, until you eventually reach object. Base class and derived class are relative terms. All classes are derived classes, either from object or from another class so generally when we call a class a derived class, we mean that it is immediately derived from some class other than object. Figure 7-3 shows a simple class hierarchy. After this, I will not show object in the figures, since all classes are ultimately derived from it.
Code 3 Of 9 Generator In C#.NET
Using Barcode generation for VS .NET Control to generate, create Code39 image in .NET framework applications.
www.OnBarcode.com
Print Identcode In Visual C#.NET
Using Barcode drawer for .NET Control to generate, create Identcode image in .NET applications.
www.OnBarcode.com
Figure 7-3. A class hierarchy
Generate Data Matrix ECC200 In Visual C#.NET
Using Barcode maker for .NET framework Control to generate, create Data Matrix 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Draw Data Matrix 2d Barcode In None
Using Barcode generation for Font Control to generate, create Data Matrix 2d barcode image in Font applications.
www.OnBarcode.com
CHAPTER 7 CLASSES AND INHERITANCE
Creating UPC-A Supplement 2 In Objective-C
Using Barcode generation for iPad Control to generate, create UPC-A Supplement 2 image in iPad applications.
www.OnBarcode.com
QR Code JIS X 0510 Creator In None
Using Barcode creation for Microsoft Excel Control to generate, create QR Code image in Office Excel applications.
www.OnBarcode.com
Hiding Members of a Base Class
Drawing Barcode In None
Using Barcode encoder for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Recognizing ANSI/AIM Code 128 In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Although a derived class cannot delete any of the members it has inherited, it can hide them. You can hide, or mask, an inherited function member by declaring a new function member with the same signature. Remember that the signature consists of the name and parameter list, but does not include the return type. To hide an inherited data member, declare a new member of the same type and with the same name. To let the compiler know that you are purposely hiding an inherited member, use the new modifier. Without it, the program will compile successfully, but the compiler will give you a warning that you are hiding an inherited member. The following code declares a base class and a derived class, each with a string member called Field1. The keyword new is used to explicitly tell the compiler to mask the base class member. Figure 7-4 illustrates an instance of each class. class SomeClass { string Field1; ... } class OtherClass : SomeClass { new string Field1; Keyword // Base class
GTIN - 12 Decoder In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
GS1 128 Maker In Java
Using Barcode creation for BIRT Control to generate, create UCC - 12 image in BIRT reports applications.
www.OnBarcode.com
// Derived class // Mask base member with same name.
GTIN - 13 Generation In Java
Using Barcode maker for Java Control to generate, create EAN 13 image in Java applications.
www.OnBarcode.com
Scan PDF417 In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Figure 7-4. Hiding a member of a base class
UPC-A Supplement 5 Generator In Java
Using Barcode encoder for Java Control to generate, create UPC-A Supplement 2 image in Java applications.
www.OnBarcode.com
Barcode Generator In VB.NET
Using Barcode generator for .NET framework Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
CHAPTER 7 CLASSES AND INHERITANCE
In the following code, OtherClass derives from SomeClass but hides both its inherited members. Note the use of the new modifier. The code is illustrated in Figure 7-5. class SomeClass { public string Field1 = "SomeClass Field1"; public void Method1(string value) { Console.WriteLine("SomeClass.Method1: } // Base class
{0}", value); }
class OtherClass : SomeClass // Derived class { Keyword new public string Field1 = "OtherClass Field1"; // Mask the base member. new public void Method1(string value) // Mask the base member. { Console.WriteLine("OtherClass.Method1: {0}", value); } } Keyword
class Program { static void Main() { OtherClass oc = new OtherClass(); oc.Method1(oc.Field1); } }
// Use the masking member. // Use the masking member.
Figure 7-5. Hiding a field and a method of the base class
CHAPTER 7 CLASSES AND INHERITANCE
Base Access
Sometimes, your derived class might need to access a hidden inherited member. You can access a hidden base class member by using a base access expression. This expression consists of the keyword base, followed immediately by a period and the name of the member, as shown here. Console.WriteLine("{0}", base.Field1); Base access For example, in the following code, derived class OtherClass hides Field1 in its base class but accesses it by using a base access expression. class SomeClass { // Base class public string Field1 = "Field1 -- In the base class"; } class OtherClass : SomeClass { // Derived class
new public string Field1 = "Field1 -- In the derived class"; Hides the field in the base class public void PrintField1() { Console.WriteLine("{0}", Field1); // Access the derived class. Console.WriteLine("{0}", base.Field1); // Access the base class. } } Base access class Program { static void Main() { OtherClass oc = new OtherClass(); oc.PrintField1(); } } This code produces the following output: Field1 -- In the derived class Field1 -- In the base class
Copyright © OnBarcode.com . All rights reserved.