Professional Documents
Culture Documents
Chapter 8
Chapter 8
Chapter 8
Inheritance in C++
Public inheritance
E.g.,
class circle: public Shape { };
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
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
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
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
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
ScrollBar
TextComponent
Label
Button
CheckBox
TextArea
TextField
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(); } }
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 { .. }
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) {} }
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 } }
Better solution, abstract out common parts to new parent class, and use subclassing for specialization
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