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:
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, but are not accessible within the child class
Private, 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, 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
Inheritance in C++ Public inheritance E.g., 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 class circle: public Shape { …… };
Inheritance in C++ (Cont.) Protected inheritance E.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 class circle: protected Shape { …… };
Inheritance in C++ (cont.) Public inheritance E.g., 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 class circle: private Shape { …… };
Constructors in Inheritance (Java) Whenever a class definition does not have a constructor, Java automatically creates on with zero parameters (default constructor) However, once you add at least one constructor to a class, then no constructors are created automatically Constructor methods are not inherited even though they are designated as "public" But, the subclass surly can use "super" to call superclass' constructors If subclass' constructor doesn't call any superclass' constructor, 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, when you add a constructor with at least one parameter and do not add one with zero parameters), you will meet a compilation error
Constructors in Inheritance (C++) When you instantiate a class object that has been derived from another class, a constructor for the base class is called first, followed by the derived class constructor, even if the base and derived class both have only default constructors If a base class does not contain a default constructor, then you must provide a constructor for every derived class, even if the derived class does not need a constructor for any other reason
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, the child is a contraction of the parent (smaller) This interplay between inheritance and overriding, extension and contraction, is what allows object-oriented systems to take very general tools and specialize them for specific projects. This interplay is ultimately the source of a great deal of the power of OOP
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''. If it “sounds right'' to your ear, 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, and therefore inheritance is not natural. But a car has-a engine
Reuse of Code, Reuse of Concept Why do we use inheritance? Basically there are two major motivations: Reuse of code. Methods defined in the parent can be made available to the child without rewriting. Makes it easy to create new abstractions Reuse of concept. Methods described in the parent can be redefined and overridden in the child. Although no code is shared between parent and child, the concept embodied in the definition is shared. An example of the latter from the case study in chapter 7, all graphical objects know how to draw.
Syntax for Inheritance Languages use a variety of different syntax to indicate inheritance: class Wall : public GraphicalObject -- c++ class Wall extends GraphicalObject -- Java class Wall : GraphicalObject -- C# (defclass Wall (GraphicalObject) () ) -- CLOS type Wall = object (GraphicalObject) -- Object Pascal class Wall < GraphicalObject -- Ruby
Trees vs Forests There are two common views of class hierarchies: All classes are part of a single large class hierarchy. Thus, there is one class that is the original ancestor of all other classes Smalltalk, 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++, Objective-C, and Apple Object Pascal do this
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 Label Button TextComponent TextArea TextField CheckBox ScrollBar
Specification Inheritance If the parent class is abstract, we often say that it is providing a specification for the child class, and therefore it is specification inheritance (a variety of specialization inheritance) Example: Java Event Listeners ActionListener, MouseListener, and so on specify behavior, but must be subclassed
Example Public interface ActionListener { public void actionPerformed(ActionEvent e) {}; } public class Beeper ... implements ActionListener { ... //where initialization occurs: button.addActionListener(this); ... public void actionPerformed(ActionEvent e) Toolkit.getDefaultToolkit().beep(); }
Inheritance for Construction If the parent class is used as a source for behavior, 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, since it can break the principle of substitutability, but nevertheless sometimes found in practice (More often in dynamically typed languages, such as Smalltalk) An example might be subclassing the idea of a Set from an existing List class
Example public class Set … { public void clear() { … } { … } public boolean isEmpty() public void add(E e) public E remove(E e) …… public class List ... extends Set { ….. }
Inheritance for Generalization or Extension If a child class generalizes or extends the parent class by providing more functionality, but does not override any method, we call it inheritance for generalization The child class doesn't change anything inherited from the parent, 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) {…} }
Inheritance for Limitation If a child class overrides a method inherited from the parent in a way that makes it unusable (for example, issues an error message), then we call it inheritance for limitation Generally not a good idea, since it breaks the idea of substitution. But again, it is sometimes found in practice For example, you have an existing List data type that allows items to be inserted at either end, and you override methods allowing insertion at one end in order to create a Stack
Example public class List … { public void insert (E element, Position p) { if ((!this.isEmpty()) && (p == front)) //insert the element in the front if ((!this.isEmpty()) && (p == end)) //insert the element in the end …… } public class Stack extends List { public void insert (E element) { if (!this.isEmpty()) //insert the element in the top …… }
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, and use subclassing for specialization
Summary of Forms of Inheritance Specialization The child class is a special case of the parent class; in other words, the child class is a subtype of the parent class. Specification The parent class defines behavior that is implemented in the child class but not in the parent class. Construction The child class makes use of the behavior provided by the parent class, but is not a subtype of the parent class. Generalization The child class modifies or overrides some of the methods of the parent class. Extension The child class adds new functionality to the parent class, but does not change any inherited behavior. Limitation The child class restricts the use of some of the behavior inherited from the parent class. Variance The child class and parent class are variants of each other, and the class-subclass relationship is arbitrary. Combination The child class inherits features from more than one parent class. This is multiple inheritance and will be the subject of a later chapter.
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
Chapter Summary In this chapter we have begun the exploration of inheritance, a topic we will continue through the next several chapters. 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