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

Generator Code 128A in Visual C# 11: Inheritance and Polymorphism

11: Inheritance and Polymorphism
Generate USS Code 128 In C#.NET
Using Barcode generation for .NET Control to generate, create Code 128C image in .NET framework applications.
www.OnBarcode.com
Code 128 Code Set A Scanner In C#
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
You also saw a lot of overloading in this chapter, and you can see how derived classes can build in their parents method implementations to create new and different methods. In the next chapter, you ll take that to the extreme, and see that you can even override simple operators, such as + and -, in almost the same way as you did with methods.
Paint Barcode In C#
Using Barcode creator for .NET framework Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Print Barcode In C#
Using Barcode generator for Visual Studio .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
Test Your Knowlege: Quiz
Creating 1D In C#.NET
Using Barcode generation for .NET Control to generate, create Linear image in .NET framework applications.
www.OnBarcode.com
DataMatrix Creator In Visual C#
Using Barcode drawer for .NET Control to generate, create DataMatrix image in VS .NET applications.
www.OnBarcode.com
Question 11-1. What is the relationship between specialization and generalization Question 11-2. How is specialization implemented in C# Question 11-3. What is the syntax for inheritance in C# Question 11-4. How do you implement polymorphism Question 11-5. What are the two meanings of the keyword new Question 11-6. How do you call a base class constructor from a derived class Question 11-7. What is an abstract method Question 11-8. What is a sealed class Question 11-9. What is the base class of Int32 Question 11-10. What is the base class of any class you create if you do not otherwise indicate a base class
PDF417 Creator In Visual C#
Using Barcode creation for Visual Studio .NET Control to generate, create PDF 417 image in .NET framework applications.
www.OnBarcode.com
Encode ISBN - 13 In Visual C#
Using Barcode creator for .NET framework Control to generate, create ISBN - 13 image in .NET framework applications.
www.OnBarcode.com
Test Your Knowledge: Exercises
Scanning Code 128B In Visual Basic .NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Draw Code-128 In None
Using Barcode generation for Office Word Control to generate, create Code 128 Code Set B image in Word applications.
www.OnBarcode.com
Exercise 11-1. Create a base class, Telephone, and derive a class ElectronicPhone from it. In Telephone, create a protected string member phonetype and a public method Ring( ) which outputs a text message such as this: Ringing the <phonetype>. In ElectronicPhone, the constructor should set the phonetype to Digital. In the Run( ) method, call Ring( ) on the ElectronicPhone to test the inheritance. Exercise 11-2. Extend Exercise 11-1 to illustrate a polymorphic method. Have the derived class override the Ring( ) method to display a different message.
Create UCC - 12 In VS .NET
Using Barcode drawer for ASP.NET Control to generate, create GTIN - 128 image in ASP.NET applications.
www.OnBarcode.com
Barcode Recognizer In VB.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Test Your Knowledge: Exercises |
Create Barcode In Java
Using Barcode printer for BIRT reports Control to generate, create Barcode image in BIRT applications.
www.OnBarcode.com
Code-39 Reader In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Exercise 11-3. Change the Telephone class to abstract, and make Ring( ) an abstract method. Derive two new classes from Telephone: DigitalPhone and TalkingPhone. Each derived class should set the phonetype, and override the Ring( ) method. Exercise 11-4. Phones these days do a lot more than ring, as you know. Add a method to DigitalPhone called VoiceMail( ) that outputs the message You have a message. Press Play to retrieve. Now add a new class, DigitalCellPhone, that derives from DigitalPhone and implements a version of VoiceMail( ) that outputs the message You have a message. Call to retrieve.
QR Maker In Java
Using Barcode creation for Java Control to generate, create QR Code ISO/IEC18004 image in Java applications.
www.OnBarcode.com
Creating Code 39 In Java
Using Barcode creation for Java Control to generate, create Code 39 image in Java applications.
www.OnBarcode.com
|
UPC-A Supplement 5 Generator In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create UPC Code image in ASP.NET applications.
www.OnBarcode.com
Creating ECC200 In Java
Using Barcode encoder for Java Control to generate, create Data Matrix 2d barcode image in Java applications.
www.OnBarcode.com
11: Inheritance and Polymorphism
QR Code Encoder In None
Using Barcode creator for Online Control to generate, create QR Code ISO/IEC18004 image in Online applications.
www.OnBarcode.com
DataMatrix Printer In Objective-C
Using Barcode generator for iPhone Control to generate, create DataMatrix image in iPhone applications.
www.OnBarcode.com
12
Operator Overloading
Back in s 3 and 4, you learned about the C# built-in types, such as integer (int) and Boolean (bool), and the various operators that let you work with those types, from the simple mathematical operators (such as + and %) to the comparison operators (== and <=) to the logical operators (&& and ||). Using most of these operators with the basic types is simple and intuitive. If you try to use those operators with classes you ve created in the past few chapters, though, you ll get an error. Back in 8, you saw how to overload the methods of your class, giving them additional functions, depending on the parameters. C# lets you extend that overloading ability to operators arithmetic ones, comparison ones, and even the operator for casting one type to another which is what we ll show you in this chapter. Although being able to overload the arithmetic operators is great, it s the equality operators that are really useful to overload, as you ll see.
Designing the Fraction Class
For example, suppose you define a type to represent fractional numbers; you might reasonably name it Fraction. The following constructors establish two Fraction objects, the first representing 1/2 and the second representing 3/4:
Fraction firstFraction = new Fraction(1,2); // create 1/2 Fraction secondFraction = new Fraction(3,4); // create 3/4
It s reasonable to create this class so that the first parameter will represent the numerator and the second parameter will represent the denominator. In general, when you create your classes, you should stick to an obvious and intuitive interpretation whenever you can. If you want your Fraction class to have all the functionality of the built-in types, you ll need to be able to perform arithmetic on instances of your fractions (add two fractions, multiply them, and so on). You should also be able to convert fractions to and from built-in types, such as int.
Hypothetically, you could implement methods for each of these operations. For example, for your Fraction type, you might create an Add( ) method, which you would invoke like this:
// add 1/2 and 3/4 Fraction theSum = firstFraction.Add(secondFraction);
This works just fine, but it s not very obvious. It s hard to read, and it s not how the user would automatically expect addition to work. It also doesn t look like addition of the built-in types, such as int. It would be much better to be able to write:
// add 1/2 and 3/4 using + operator Fraction theSum = firstFraction + secondFraction;
Statements that use operators (in this case, the plus sign) are intuitive and easy to use. Equally important, this use of operators is consistent with how built-in types are added, multiplied, and so forth. The C# syntax for overloading an operator is to write the keyword operator followed by the operator to overload. The next section demonstrates how you might do this for the Fraction class.
Copyright © OnBarcode.com . All rights reserved.