CSSE501 Object-Oriented Development

Chapter 8: Inheritance and Substitution
In this chapter we will start to investigate the concepts of inheritance and substitution 
The intuitive and practical meanings of inheritance  The syntax used to describe inheritance and substitution  Some of the various forms of inheritance  The benefits and costs of inheritance

Abstract Idea of Inheritance
We motivated the idea of inheritance with a hierarchy of categories:

but are not accessible within the child class .Practical Meaning of Inheritance Data members in the parent are part of the child Behavior defined in the parent are part of the child Note that private aspects of the parent are part of the child.

just not accessible)  public: accessible anywhere  protected: accessible within the class definition or within the definition of child classes Note: Java interprets protected to mean accessible within same package . Public and Protected There are now three levels of visibility modifiers:  private: accessible only within the class definition (but memory is still found in the child class.Private.

 Public base class members remain public in the derived class  Protected base class members remain Protected in the derived class  Private base class members remain private in the derived class .Inheritance in C++ Public inheritance  E. class circle: public Shape { «« }.g..

class circle: protected Shape { «« }.Inheritance in C++ (Cont..g.  Public base class members become protected in the derived class  Protected base class members remain protected in the derived class  Private base class members are inaccessible in the derived class .) Protected inheritance  E.

 Public base class members become private in the derived class  Protected base class members remain private in the derived class  Private base class members are inaccessible in the derived class .) Public inheritance  E. class circle: private Shape { «« }.g..Inheritance in C++ (cont.

when you add a constructor with at least one parameter and do not add one with zero parameters). then no constructors are created automatically Constructor methods are not inherited even though they are designated as "public"  But. you will meet a compilation error . once you add at least one constructor to a class. Java will automatically call superclass' default constructor (with zero parameter) before executing the lines in the subclass' constructor If the superclass happens to have no default constructor (for example.Constructors in Inheritance (Java) Whenever a class definition does not have a constructor. Java automatically creates on with zero parameters (default constructor)  However. the subclass surly can use "super" to call superclass' constructors If subclass' constructor doesn't call any superclass' constructor.

even if the base and derived class both have only default constructors If a base class does not contain a default constructor.Constructors in Inheritance (C++) When you instantiate a class object that has been derived from another class. followed by the derived class constructor. then you must provide a constructor for every derived class. a constructor for the base class is called first. even if the derived class does not need a constructor for any other reason .

This interplay is ultimately the source of a great deal of the power of OOP . the child is a contraction of the parent (smaller) This interplay between inheritance and overriding.Inheritance is both Extension and Contraction Because the behavior of a child class is strictly larger than the behavior of the parent. the child is an extension of the parent (larger) Because the child can override behavior to make it fit a specialized situation. is what allows objectoriented systems to take very general tools and specialize them for specific projects. extension and contraction.

then A can be made a subclass of B A dog is-a mammal. and therefore a dog inherits from mammal A car is-a engine sounds wrong. If it ³sounds right'' to your ear. and therefore inheritance is not natural.The is-a Rule Our idealization of inheritance is captured in a simple rule-of-thumb Try forming the English sentences ³An A is-a B''. But a car has-a engine .

Makes it easy to create new abstractions  Reuse of concept. Although no code is shared between parent and child. An example of the latter from the case study in chapter 7. Methods described in the parent can be redefined and overridden in the child.Reuse of Code. Reuse of Concept Why do we use inheritance? Basically there are two major motivations:  Reuse of code. . the concept embodied in the definition is shared. Methods defined in the parent can be made available to the child without rewriting. all graphical objects know how to draw.

Java class Wall : GraphicalObject -.C# (defclass Wall (GraphicalObject) () ) -.c++ class Wall extends GraphicalObject -.Ruby .Object Pascal class Wall < GraphicalObject -.Syntax for Inheritance Languages use a variety of different syntax to indicate inheritance: class Wall : public GraphicalObject -.CLOS type Wall = object (GraphicalObject) -.

and Apple Object Pascal do this . there is one class that is the original ancestor of all other classes Smalltalk. Thus. Objective-C.Trees vs Forests There are two common views of class hierarchies:  All classes are part of a single large class hierarchy. Java and Delphi Pascal do this  Classes are only placed in hierarchies if they have a relationship .results in a forest of many small hierarchies. but no single ancestor C++.

Forms of Inheritance Inheritance can be used in a variety of different ways and for different purposes Many of these types of inheritance are given their own special names We will describe some of these specialized forms of inheritance       Specialization Specification Construction Generalization or Extension Limitation Variance .

Specialization Inheritance By far the most common form of inheritance is specialization Each child class overrides a method inherited from the parent in order to specialize the class in some way A good example is the Java hierarchy of graphical components in the Abstract Windowing Toolkit (AWT): Component ScrollBar TextComponent Label Button CheckBox TextArea TextField .

and therefore it is specification inheritance (a variety of specialization inheritance) Example: Java Event Listeners ActionListener. and so on specify behavior.Specification Inheritance If the parent class is abstract. but must be subclassed . MouseListener. we often say that it is providing a specification for the child class.

getDefaultToolkit()... } public class Beeper . } } ... public void actionPerformed(ActionEvent e) { Toolkit..addActionListener(this). //where initialization occurs: button. .Example Public interface ActionListener { public void actionPerformed(ActionEvent e) {}..beep(). implements ActionListener { .

such as Smalltalk) An example might be subclassing the idea of a Set from an existing List class .Inheritance for Construction If the parent class is used as a source for behavior. since it can break the principle of substitutability. but nevertheless sometimes found in practice (More often in dynamically typed languages. but the child class has no is-a relationship to the parent. then we say the child class is using inheritance for construction Generally not a good idea.

.. extends Set { «. } .Example public class Set « { public void clear() { « } public boolean isEmpty() { « } public void add(E e) { « } public E remove(E e) { « } «« } public class List ..

but does not override any method. we call it inheritance for generalization The child class doesn't change anything inherited from the parent.Inheritance for Generalization or Extension If a child class generalizes or extends the parent class by providing more functionality. it simply adds new features An example is Java Properties inheriting form Hashtable .

Example public class Hashtable « { public int size() { « } public boolean isEmpty() { « } public Enumeration keys() { « } public Enumeration elements() { « } «« } public class Properties . extends Hashtable { public Object setProperty (String key. String value) {«} public void load(InputStream inStream) throws IOException {«} public void save (OutputStream out... String header) {«} } .

issues an error message). since it breaks the idea of substitution. it is sometimes found in practice For example. then we call it inheritance for limitation Generally not a good idea. and you override methods allowing insertion at one end in order to create a Stack . But again.Inheritance for Limitation If a child class overrides a method inherited from the parent in a way that makes it unusable (for example. you have an existing List data type that allows items to be inserted at either end.

isEmpty()) //insert the element in the top «« } } .Example public class List « { public void insert (E element.isEmpty()) && (p == end)) //insert the element in the end «« } } public class Stack extends List { public void insert (E element) { if (!this. Position p) { if ((!this.isEmpty()) && (p == front)) //insert the element in the front if ((!this.

and use subclassing for specialization .Inheritance for Variance Two or more classes that seem to be related. but its not clear who should be the parent and who should be the child Example: Mouse and Touchpad and Joystick Better solution. abstract out common parts to new parent class.

The child class adds new functionality to the parent class. This is multiple inheritance and will be the subject of a later chapter. in other words. The child class inherits features from more than one parent class.Summary of Forms of Inheritance Specialization         The child class is a special case of the parent class. The child class restricts the use of some of the behavior inherited from the parent class. The child class and parent class are variants of each other. the child class is a subtype of the parent class. The parent class defines behavior that is implemented in the child class but not in the parent class. but does not change any inherited behavior. The child class makes use of the behavior provided by the parent class. Specification Construction Generalization Extension Limitation Variance Combination . The child class modifies or overrides some of the methods of the parent class. and the class-subclass relationship is arbitrary. but is not a subtype of the parent class.

Benefits of Inheritance Software Reuse Code Sharing Consistency of Interface Software Components Rapid Prototyping Polymorphism Information Hiding .

Cost of Inheritance Execution speed Program size Message Passing Overhead Program Complexity This does not mean you should not use inheritance. but rather than you must understand the benefits. and weigh the benefits against the costs .

Topics we have addressed have included the following:  The meaning of inheritance  The syntax used to describe inheritance and overriding  The idea of substitution of a child class for a parent  The various forms of inheritance  The cost and benefits of inheritance .Chapter Summary In this chapter we have begun the exploration of inheritance. a topic we will continue through the next several chapters.