Kurt Nørmark ©
Department of Computer Science, Aalborg University, Denmark
Abstract Previous lecture Next lecture Index References Contents | In this chapter of the material we study inheritance. At the conceptual level, we start by a study of class specialization and class extension. Following that the programming language concept of inheritance is introduced. In the last part of the chapter we study inheritance in C#. This includes polymorphism, dynamic binding, abstract classes, and a number of design patterns for which inheritance is of central importance. |
Specialization of Classes |
Specialization of Classes Slide Annotated slide Contents Index References Textbook |
|
|
The class B is a specialization of class A |
The concept specialization: If a class B is a specialization of a class A then
|
The extension of class specialization Slide Annotated slide Contents Index References Textbook |
|
|
The extension of a class A is narrowed when the class is specialized to B |
|
|
|
Example: Bank Accounts Slide Annotated slide Contents Index References Textbook |
A specialization hierarchy of bank accounts |
Possible extensions of the bank account classes |
Example: Bank Accounts in C# Slide Annotated slide Contents Index References Textbook |
|
Program: The base class BankAccount. |
|
Program: The class CheckAccount. |
|
Program: The class SavingsAccount. |
|
Program: The class LotteryAccount. |
|
Program: The singleton class Lottery - used by LotteryAccount. |
|
Example: Geometric Shapes Slide Annotated slide Contents Index References Textbook |
A specialization hierarchy of polygons |
Exercise 7.2. Polygons, Quadrangles and Squares | The purpose of this exercise is to get some experience with programming of classes in a pure specialization hierarchy. I have programmed the class Polygon. The well-known class Point, which we have seen several times during the course, is used as a building block of class Polygon type. (Notice that class Point has a MidPoint operation and a RotateAroundPoint operation). The class Polygon has the following public operations:
Now program the following specializations of Polygon:
For the sake of simplicity you are allowed (but not forced) to assume that the edges of the square are parallel with the x-axis and the y-axis. Please refer to the Polygon type hierarchy on the accompanying slide. Quadrangle and Square should support the same operations as Polygon. In particular Quadrangle and Square should support adequate constructors of their own. It is not necessary to use four points for representation of a square. Discuss this problem and try to find a solution. |
Specialization of classes Slide Annotated slide Contents Index References Textbook |
|
|
|
The Principle of Substitution Slide Annotated slide Contents Index References Textbook |
|
The class B is a specialization of class A |
The concept principle of substitution: If B is a subclass of A, it is possible to substitute an given instance of B in place of an instance of A without observable effect |
|
Extension of Classes |
Extension of Classes Slide Annotated slide Contents Index References Textbook |
|
The class B is a specialization of class A |
The concept extension: If class B is an extension of class A then
|
An example of simple extension Slide Annotated slide Contents Index References Textbook |
|
Program: The class Point2D. |
|
Program: The class Point3D which extends class Point3d. |
|
Program: A client of the classes Point2D and Point3d. |
|
Program: The output from the Client program. |
|
Exercise 7.3. Point3D: A client or a subclass of Point2D? | The purpose of this exercise is to sharpen your understanding of the difference between "being a client of class C" and "being af subclass of class C". The class Point3D extends Point2D by means of inheritance. As an alternative, the class Point3D may be implemented as a client of Point2D. In more practical terms this means that the class Point3D has an instance variable of type Point2D. Now implement Point3D as a client of Point2D - such that a 3D point has a 2D point as a part. Be sure that the class Point3D has the same interface as the version of class Point3D from the course material. Evaluate the difference between "being a client of" an "extending" class Point2D. Which of the solutions do you prefer? |
|
|
The intension of class extensions Slide Annotated slide Contents Index References Textbook |
|
|
The intension of a class A is blown up when the class is extended to B |
|
Inheritance in General |
Inheritance Slide Annotated slide Contents Index References Textbook |
|
Two classes B and C that inherit from class A |
|
Interfaces to clients and subclasses Slide Annotated slide Contents Index References Textbook |
Figure. Interfaces between A, B, their client classes, and their subclasses
|
Visibility and Inheritance Slide Annotated slide Contents Index References Textbook |
|
|
Class hierarchies and Inheritance Slide Annotated slide Contents Index References Textbook |
|
Different graph structures among classes |
|
Multiple inheritance Slide Annotated slide Contents Index References Textbook |
|
|
|
Problems with multiple inheritance Slide Annotated slide Contents Index References Textbook |
|
Class B is a subclass of class A |
|
|
Inheritance in C# |
Class Inheritance in C# Slide Annotated slide Contents Index References Textbook |
Syntax: A C# class defined as a subclass of given superclass |
|
Program: A class A and its subclass B. Or a class B and its base class A. |
|
The class B inherits from class A |
|
The top of the class hierarchy Slide Annotated slide Contents Index References Textbook |
|
The overall type hierarchy in C# |
Methods in the class Object in C# Slide Annotated slide Contents Index References Textbook |
|
|
|
Inheritance and Constructors Slide Annotated slide Contents Index References Textbook |
|
|
|
Program: Constructors in class BankAccount. |
|
Program: Constructors in class CheckAccount. |
|
Program: Constructors in class SavingsAccount. |
|
Program: Constructors in class LotteryAccount. |
|
Constructors and initialization order Slide Annotated slide Contents Index References Textbook |
|
|
Constructors and initialization order: Example Slide Annotated slide Contents Index References Textbook |
|
Program: Initializers and constructors of class C. |
|
Program: Initializers and constructors of class B. |
|
Program: Initializers and constructors of class A. |
|
Program: The class Init and the method InitMe. |
|
Program: A program that instantiates and initializes class C. |
|
Program: The output that reveals the initialization order. |
|
Visibility modifiers in C# Slide Annotated slide Contents Index References Textbook |
|
|
|
Exercise 7.5. Private Visibility and inheritance | Take a look at the classes shown below:
Answer the following questions before you run the program:
Run the program and confirm your answers. |
Exercise 7.5. Internal Visibility | The purpose of this exercise is to get some experience with the visibility modifier called internal. Take a look at the slide to which this exercise belongs. In this exercise, it is recommended to activate the compiler from a command prompt. Make a namespace N with two classes P and I:
Compile the classes in the namespace N to a single assembly, for instance located in the file x.dll. Demonstrate that the class I can be used in class P. Also demonstrate that P.i can be seen and used in class P. After this, program a class A, which attempts to use the classes P and I from x.dll. Arrange that class A is compiled separately, to a file y.dll. Answer the following questions about class A:
Finally, arrange that class A is compiled together with N.P and N.I to a single assembly, say y.dll. Does this alternative organization affect the answers to the questions asked above? |
Inheritance of methods, properties, and indexers Slide Annotated slide Contents Index References Textbook |
|
|
|
|
Inheritance of methods: Example. Slide Annotated slide Contents Index References Textbook |
|
Program: The base class BankAccount. |
|
Program: The class CheckAccount. |
|
Program: The class SavingsAccount. |
|
Program: The class LotteryAccount. |
|
Program: The class Lottery. |
|
Exercise 7.6. A subclass of LotteryAccount | On the slide, to which this exercise belongs, we have emphasized inheritance of methods and properties in the bank account class hierarchy. From the web-version of the material there is direct access to the necessary pieces of program. The LotteryAccount uses an instance of a Lottery object for adding interests. Under some lucky circumstances, the owner of a LotteryAccount will get a substantial amount of interests. In most cases, however, no interests will be added. There exists a single file which contains the classes BankAccount, CheckAccount, SavingsAccount, Lottery, together with a sample client class. Program a specialization of the LotteryAccount, called LotteyPlusAccount, with the following redefinitions of Deposit and Withdraw.
Notice that the Deposit and Withdraw methods in LotteryPlusAccount should combine with the method in LotteryAccount (method combination). Thus, use the Deposit and Withdraw methods from LotteryAccount as much as possible when you program the LotteryPlusAccount. Test-drive the class LotteryPlusAccount from a sample client class. |
Overriding and Hiding in C# Slide Annotated slide Contents Index References Textbook |
|
Program: Two methods M in classes A and B, where B inherits from A. |
|
|
Polymorphism. Static and dynamic types Slide Annotated slide Contents Index References Textbook |
|
The concept polymorphism: Polymorphism stands for the idea that a variable can refer to objects of several different types | ||
The concept static type: The static type of a variable is the type of variable, as declared | ||
The concept dynamic type: The dynamic type of a variable is type of object to which the variable refers | ||
The concept dynamic binding: Dynamic binding is in effect if the dynamic type of a variable v determines the operation activated by v.op(...) |
|
Static and dynamic types in C# Slide Annotated slide Contents Index References Textbook |
|
Program: Illustration of static and dynamic types. |
|
Program: Corrections of the errors in the illustration of static and dynamic types. |
|
Type test and type conversion in C# Slide Annotated slide Contents Index References Textbook |
|
|
|
|
|
Examples of type test and type conversion Slide Annotated slide Contents Index References Textbook |
|
Program: An illustration of dynamic type run-time check with v is C. |
|
Program: Output of dynamic type run-time checking program . |
|
Program: An illustration of type casting (C)v. |
|
Exercise 7.8. Casting of BankAccounts | In the program "An illustration of type casting (C)v" the static types of the variables baRes1 .. baRes6 are BankAccount. Now change the static types of the variables baRes1 .. baRes6 to CheckAccount. This is done simply by declaring baRes1 .. baRes6 as CheckAccounts. Predict the compilers reaction before you attempt a compilation of the modified program. Afterwards compile the program and confirm your predictions. |
Program: An illustration of type conversion with v as C. |
|
Program: Output of dynamic type type conversion program . |
|
Exercise 7.8. Static and dynamic types | Type conversion with v as T was illustrated with a program on the accompanying slide. The output of the program was confusing and misleading. We want to report the static types of the expressions ba1 as BankAccount, ba1 as CheckAccount, etc. If you access this exercise from the web-version there will be direct links to the appropriate pieces of program. Explain the output of the program. You can examine the classes BankAccount, CheckAccount, SavingsAccount and LotteryAccount, if you need it. Modify the program such that the static type of the expressions bai as BanktypeAccount is reported. Instead of baRes1 = ba1 as BankAccount; Report(baRes1); you should activate some method on the expression ba1 as BankAccount which reveals its static type. In order to do so, it is allowed to add extra methods to the bank account classes. |
Virtual methods in C# Slide Annotated slide Contents Index References Textbook |
|
Program: An illustration of virtual and new methods in class A and B. |
|
Program: An illustration of virtual and new methods in class A and B - Simplified. |
|
Program: Output from the program that illustrates virtual and new methods. |
|
Program: An illustration of virtual and new methods - More simplified. |
|
Program: Output from the more simplified program. |
|
|
Practical use of virtual methods in C# Slide Annotated slide Contents Index References Textbook |
|
|
Program: Use of virtual bank account methods. |
|
Program: Adding interests without use of dynamic binding - AddInterest is not virtual. |
|
Program: Output from the bank account programs. |
|
|
Overriding the Equals method in a class Slide Annotated slide Contents Index References Textbook |
|
|
|
Program: Equals and GetHashCode Methods in class BankAccount. |
|
Upcasting and downcasting in C# Slide Annotated slide Contents Index References Textbook |
|
A specialization hierarchy of bank accounts |
Program: An illustration of upcasting and downcasting. |
|
Upcasting and downcasting in C# Slide Annotated slide Contents Index References Textbook |
|
|
Inheritance and Variables Slide Annotated slide Contents Index References Textbook |
|
|
Program: An illustration of "non-virtual variable access". |
|
Program: Similar illustration with virtual properties. |
|
Program: Classes for exercise. |
|
Exercise 7.9. Non-virtual variables - Virtual Methods | Take a look at the following program
and figure out what it prints. Explain the behaviour. |
|
Chapter 7: Specialization, Extension, and Inheritance
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: February 7, 2011, 12:16:37