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. |
Method Combination |
Method Combination Slide Annotated slide Contents Index References Textbook |
|
Class B is a subclass of class A |
|
|
Parameter Variance Slide Annotated slide Contents Index References Textbook |
|
Class B is a subclass of class A, and T is a subclass of S. |
Program: An illustration of the problems with covariance. |
|
Program: The full C# program. |
|
|
Covariance and Contravariance Slide Annotated slide Contents Index References Textbook |
|
Class B is a subclass of class A, and the parameter class T is a subclass of S. |
|
Class B is a subclass of class A, and the parameter class S is a subclass of T. |
Exercise 8.2. Parameter variance | First, be sure you understand the co-variance problem stated above. Why is it problematic to execute aref.Op(sref)in the class Client? The parameter variance problem, and the distinction between covariance and contravariance, is not really a topic in C#. The program with the classes A/B/S/T on the previous page compiles and runs without problems. Explain why! |
Abstract Classes - Sealed Classes |
Abstract Classes Slide Annotated slide Contents Index References Textbook |
|
The concept abstract class: An abstract class is a class with one or more abstract operations | ||
The concept abstract operation: An abstract operation is specially marked operation with a name and with formal parameters, but without a body |
|
Abstract classes and abstract methods in C# Slide Annotated slide Contents Index References Textbook |
|
Program: An abstract class Stack - without data representation - with a non-abstract ToggleTop method. |
|
|
Exercise 8.4. A specialization of Stack | On the slide to which this exercise belongs, we have shown an abstract class Stack. It is noteworthy that the abstract Stack is programmed without any instance variables (that is, without any data representation of the stack). Notice also that we have been able to program a single non-abstract method ToggleTop, which uses the abstract methods Top, Pop, and Push. Make a non-abstract specialization of Stack, and decide on a reasonable data representation of the stack. In this exercise it is OK to ignore exception/error handling. You can, for instance, assume that the capacity of the stack is unlimited; That popping an empty stack an empty stack does nothing; And that the top of an empty stack returns the string "Not Possible". In a later lecture we will revisit this exercise in order to introduce exception handling. Exception handling is relevant when we work on full or empty stacks. Write a client of your stack class, and demonstrate the use of the inherited method ToggleTop. If you want, you can also adapt my stack client class . |
Exercise 8.4. Course and Project classes | In the earlier exercise about courses and projects (found in the lecture about classes) we programmed the classes BooleanCourse, GradedCourse, and Project. Revise and reorganize your solution (or the model solution) such that BooleanCourse and GradedCourse have a common abstract superclass called Course. Be sure to implement the method Passed as an abstract method in class Course. In the Main method (of the client class of Course and Project) you should demonstrate that both boolean courses and graded courses can be referred to by variables of static type Course. |
Abstract Properties Slide Annotated slide Contents Index References Textbook |
|
Program: The abstract class Point with four abstract properties. |
|
Program: A non-abstract specialization of class Point (with private polar representation). |
|
Program: Some client class of Point - Similar to a Point client from an earlier lecture. |
|
Program: Output from the Some client class of Point. |
|
|
Sealed Classes and Sealed Methods Slide Annotated slide Contents Index References Textbook |
|
|
Interfaces |
Interfaces Slide Annotated slide Contents Index References Textbook |
|
The concept interface: An interface describes signatures of operations, but it does not implement any of them |
|
Program: Sketches of the classes Vehicle, FixedProperty, Bus, and House - for the exercise. |
|
Exercise 8.5. The interface ITaxable | For the purpose of this exercise you are given a couple of very simple classes called Bus and House. Class Bus specializes the class Vehicle. Class House specializes the class FixedProperty. All the classes are here. First in this exercise, program an interface ITaxable with a parameterless operation TaxValue. The operation should return a decimal number. Next, program variations of class House and class Bus which implement the interface ITaxable. Feel free to invent the concrete taxation of houses and busses. Notice that both class House and Bus have a superclass, namely FixedProperty and Vehicle, respectively. Therefore it is essential that taxation is introduced via an interface. Demonstrate that taxable house objects and taxable bus objects can be used together as objects of type ITaxable. |
Interfaces in C# Slide Annotated slide Contents Index References Textbook |
|
Syntax: The syntax of a C# interface, together with the syntaxes of method, property, indexer, and event descriptions in an interface |
|
Examples of Interfaces Slide Annotated slide Contents Index References Textbook |
|
Program: The interface IGameObject. |
|
Program: The class Die which implements IGameObject. |
|
Program: The class Card which implements IGameObject. |
|
Program: A sample Client program of Die and Card. |
|
Program: Output from the sample Client program of Die and Card. |
|
|
Exercise 8.6. An abstract GameObject class | On the slide, to which this exercise belongs, we have written an interface IGameObject which is implemented by both class Die and class Card. Restructure this program such that class Die and class Card both inherit an abstract class GameObject. You should write the class GameObject. The client program should survive this restructuring. (You may, however, need to change the name of the type IGameObject to GameObject). Compile and run the given client program with your classes. |
Interfaces from the C# Libraries Slide Annotated slide Contents Index References Textbook |
|
|
|
Sample use of IComparable Slide Annotated slide Contents Index References Textbook |
|
Program: A reproduction of the interface IComparable. |
|
|
|
Exercise 8.7. Comparable Dice | In this exercise we will arrange that two dice can be compared to each other. The result of die1.CompareTo(die2) is an integer. If the integer is negative, die1 is considered less than die2; If zero, die1 is considered equal to die2; And if positive, die1 is considered greater than die2. When two dice can be compared to each other, it is possible sort an array of dice with the standard Sort method in C#. Program a version of class Die which implements the interface System.IComparable. Consult the documentation of the (overloaded) static method System.Array.Sort and locate the Sort method which relies on IComparable elements. Make an array of dice and sort them by use of the Sort method. |
Sample use of IEnumerator and IEnumerable Slide Annotated slide Contents Index References Textbook |
|
Program: A reproduction of the interface IEnumerable. |
|
Program: A reproduction of the interface IEnumerator. |
|
Program: IEnumerator in the type Interval. |
|
Program: Iteration with and without foreach based on the enumerator. |
|
|
|
Sample use of IFormattable Slide Annotated slide Contents Index References Textbook |
|
Program: A reproduction of the interface IFormattable. |
|
|
Program: The struct Card that implements IFormattable. |
|
Program: A client of Card which applies formatting of cards. |
|
Program: Output from the client program. |
|
Explicit Interface Member Implementations Slide Annotated slide Contents Index References Textbook |
|
Program: The class Playing card with a property Value. |
|
Program: The Interface IGameObject with a conflicting Value property. |
|
Program: A class Card which implements IGameObject. |
|
Program: Sample use of class Card in a Client class. |
|
Program: Output of Card Client. |
|
Patterns and Techniques |
The Composite design pattern Slide Annotated slide Contents Index References Textbook |
|
|
A template of the class structure in the Composite design pattern. |
|
A Composite Example: Music Elements Slide Annotated slide Contents Index References Textbook |
|
The class diagram of Music Elements |
An application of Music Elements Slide Annotated slide Contents Index References Textbook |
Program: An application of some MusicElement objects. |
|
|
A possible tree of objects which represent various music elements. Nodes named Ni are Note instances, and the node named P is a Pause instance |
|
Implementation of MusicElement classes Slide Annotated slide Contents Index References Textbook |
|
Program: The abstract class MusicElement. |
|
Program: The class Note. |
|
Program: The class Pause. |
|
Program: The class SequentialMusicElement. |
|
Program: The class ParallelMusicElement. |
|
A Composite example: IntSequence Slide Annotated slide Contents Index References |
|
The class diagram of the IntSequence composite. |
A Composite example: IntSequence application Slide Annotated slide Contents Index References |
|
Program: An application of IntSequence objects. |
|
Program: Output from the IntSequence application. |
|
A possible tree of objects which represent the integer sequence 3, 4, 5, 17, 12, 11, 10, 9, 8, 7, 29. |
Implementation of the IntSequence classes Slide Annotated slide Contents Index References |
|
Program: The abstract class IntSequence. |
|
Program: The class IntInterval. |
|
Program: The class IntSingular. |
|
Program: The class IntCompSeq. |
|
A Composite Example: A GUI Slide Annotated slide Contents Index References Textbook |
|
Program: A program that builds a sample composite graphical user interface. |
|
Figure. A Form (Window) with two buttons, a textbox, and a panel. |
A Composite Example: A GUI Slide Annotated slide Contents Index References Textbook |
|
The tree of objects behind the graphical user interface. These objects represents a composite design pattern in an executing program. |
A Composite Example: A GUI Slide Annotated slide Contents Index References Textbook |
|
An extract of the Windows Form classes for GUI building. We see two Composites among these classes. |
Cloning Slide Annotated slide Contents Index References Textbook |
The concept cloning: Cloning creates a copy of an existing object |
|
|
Cloning in C# Slide Annotated slide Contents Index References Textbook |
|
|
|
Program: A reproduction of the interface ICloneable. |
|
Program: A cloneable class Point. |
|
Program: A sample client of class Point. |
|
Program: Illegal cloning with MemberwiseClone. |
|
The fragile base class problem Slide Annotated slide Contents Index References Textbook |
|
Program: The initial program. |
|
Program: Output of original program. |
|
Program: A revised program with a new version of class A with a method M2. |
|
Program: The revised version with method A.M2 being virtual. |
|
Program: Output of revised program. |
|
Program: The revised version with method A.M2 being hidden. |
|
Program: Output of revised program. |
|
The Visitor design pattern Slide Annotated slide Contents Index References Textbook |
|
A template of the class structure in the Composite design pattern. |
|
Natural object-oriented IntSequence traversals Slide Annotated slide Contents Index References Textbook |
|
|
The class diagram of the IntSequence composite. |
Program: The abstract class IntSequence. |
|
Program: The class IntInterval. |
|
Program: The class IntSingular. |
|
Program: The class IntCompSeq. |
|
Program: A sample application of IntSequences. |
|
Program: Program output. |
|
Towards a Visitor solution Slide Annotated slide Contents Index References Textbook |
|
|
|
A Visitor example: IntSequence Slide Annotated slide Contents Index References Textbook |
Program: The Visitor Interface. |
|
Program: The abstract class IntSequence. |
|
Program: The class IntInterval. |
|
Program: The class IntSingular. |
|
Program: The class IntCompSeq. |
|
Program: The class MinVisitor. |
|
Program: The class MaxVisitor. |
|
Program: The class SumVisitor. |
|
Program: A sample application of IntSequences and visitors. |
|
Program: Program output. |
|
Visitors - Pros and Cons Slide Annotated slide Contents Index References Textbook |
|
|
|
|
Chapter 8: Abstract classes, Interfaces, and Patterns
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:17:38