CS257 Computer Science I Kevin Sahr, PhD Lecture 10: Inheritance 1 Object Oriented Features For a programming language to be called object oriented it should support the following features: 1. objects: group data and behavior together 2. encapsulation: control access to object internals through a welldefined interface 3. inheritance 4. polymorphism In this lecture we will discuss inheritance We will discuss polymorphism later 2 Inheritance Inheritance is the ability to derive a new class from an existing one The existing class is called the parent class, superclass, or base class The derived class is called the child class, subclass, or derived class The child class inherits the methods and data defined by the parent class the parent class methods and data are available in the child class just as if they had been declared there (with some exceptions we will discuss) 3 Inheritance Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class Animal Snake Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parent EX: a Snake is-a Animal Good design requires that inheritance only be used in situations where an is-a sentence makes sense 4
Inheritance A programmer can tailor a derived class as needed by adding new variables or methods, or by modifying the inherited ones Software reuse is a fundamental benefit of inheritance By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software Inheritance also enhances maintainability fixing a bug in the base class automatically fixes the error for all subclasses adding a feature to the base class automatically adds that feature to all sub-classes 5 Deriving Subclasses In Java, we use the reserved word extends to establish an inheritance relationship public class Snake extends Animal { // class contents } See Animal.java and Snake.java 6 The protected Modifier Visibility modifiers affect the way that class members can be used in a child class Variables and methods declared with private visibility cannot be referenced by name in a child class They can be referenced in the child class if they are declared with public visibility -- but public variables violate the principle of encapsulation There is a third visibility modifier that helps in inheritance situations: protected 7 The protected Modifier The protected modifier allows a child class to reference a variable or method directly in the child class It provides more encapsulation than public visibility, but is not as tightly encapsulated as private visibility A protected variable is visible to any class in the same package as the parent class protected variables and methods are shown with a # symbol preceding them in UML diagrams recall that - indicates private, and + indicates public 8
Visibility and Access All variables and methods of a parent class (even private ones) are inherited by its children a copy of all instance variables of all ancestor classes is allocated for each newly created child class object But as we've mentioned, private members cannot be referenced by name in the child class Because the parent can refer to it s private members, the child can reference them indirectly using the parent's methods (for example, the parent getter or setter method for an instance variable) 9 The super Reference Recall that the this reference allows an object to refer to itself Sometimes an object needs to refer to the parent s part of the object The super reference can be used to refer to the parent class object The super reference can be used to reference variables and methods defined in the parent s class (assuming they are visible to the subclass) 10 Parent Constructor Constructors are not inherited, even though they have public visibility A child class constructor is responsible for calling the parent s constructor The first line of a child s constructor should use the super reference to call the parent s constructor See Animal.java and Snake.java 11 Overriding Methods A child class can override the definition of an inherited method in favor of its own This is done by defining a new version of the method in the child class The new method must have the same signature as the parent's method, but can have a different body The class of the object executing the method determines which version of the method is invoked See Animal.java and Snake.java 12
Overriding A method defined in the parent class can always be invoked explicitly using the super reference, even if that method has been overridden in the child class EX: invoke the dosomething method as defined in my parent class: super.dosomething(); If a method is declared with the final modifier, it cannot be overridden in a subclass 13 Overloading vs. Overriding Overloading deals with multiple methods with the same name in the same class, but with different signatures Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature Overloading lets you define a similar operation in different ways for different parameters Overriding lets you define a similar operation in different ways for different object types 14 Class Hierarchies A child class of one parent can be the parent of another child, and so forth, forming a class hierarchy Business RetailBusiness ServiceBusiness KMart Macys Kinkos 15 Class Hierarchies Two children of the same parent are called siblings An inherited member is passed continually down the line Therefore, a child class inherits from all its ancestor classes 16
Class Hierarchy Design There is no single class hierarchy that is appropriate for all situations Make sure all inheritance relationships form valid is-a sentences Common features should be put as high in the hierarchy as possible this maximizes software reuse 17 The Object Class The Object class is defined in the java.lang package of the Java standard class library All classes are derived from the Object class If a class does not explicitly define a parent class, then that class is automatically a subclass of the Object class the Object class is the ultimate root of all class hierarchies because the Object class is always part of every class hierarchy it is not usually depicted in UML Class Hierarchy diagrams 18 The Object Class tostring Method The Object class contains a few useful methods, which are inherited by all classes For example, the tostring method is defined in the Object class The tostring method in the Object class is defined to return a string that contains the name of the object s class along with some other internal information Every time we define the tostring method in one of our classes, we are actually overriding the inherited definition When tostring is invoked on one of our objects, our overridden definition is executed Often we can use the parent class tostring (super.tostring()) to get the parent class portion of our String representation 19 The Object Class equals Method The equals method is defined in the Object class returns true if two references are aliases We can override equals in any class to define equality in some more appropriate way As we've seen, the String class defines the equals method to return true if two String objects contain the same characters The designers of the String class have overridden the equals method inherited from Object in favor of a more useful version 20
A Complete Example see the Animal class UML diagram and source code 21 Lecture 10 Vocabulary inheritance parent/super/base class child/sub/derived class software reuse protected super override class hierarchy sibling 22