BOOK NOTES CHAPTER 13 INHERITANCE 13.1 INTRODUCTION TO INHERITANCE - mechanism for enhancing existing classes - new class can inherit from existing class representing more general concept - syntax: defining a class that inherits from another class extends {...} - new class - specifies only new methods and instance fields - automatically inherits all methods and instance fields - superclass - more general class - basis for inheritance - subclass - more specialized class - inherits from superclass - every class that does not specifically extend another class is a subclass of Object - means all Objects have methods such as toString - UML - solid arrow with triangle tip - points to superclass - difference from implementing interface - interface has no state and no behavior - superclass has both, and subclasses inherit them - code reuse - defining subclass: specify added - instance fields - added methods - changed/overridden methods - object diagram - methods of a subclass cannot access private fields of superclass, even though subclass object still has those fields - within a subclass method, method calls without an implicit parameter still refer to the object that called the original method 13.2 INHERITANCE HIERARCHIES - represent as tree - most general concept at root - more specialized concepts toward branches - inheritance hierarchy - common way of grouping Java classes - most general class at root - more specialized classes toward branches - designing class hierarchy - determine universally common features and behaviors: superclass - subclasses - support all methods from the superclass - implementations may be modified to match their specialized purposes - free to introduce additional methods 13.3 INHERITING INSTANCE FIELDS AND METHODS - specify additional instance fields and methods of a subclass - defining methods - override methods from the superclass - specify method with same signature - overrided method, not original one, is executed - inherit methods - automatically if superclass method not overrided - can be applied to the subclass objects - define new methods - can only be applied to subclass objects - defining instance fields - can never override - subclass automatically inherits all superclass fields - define new fields: present only in subclass - define a new field with the same name as the superclass field - legal - each object would have two fields with the same name - the fields can hold different values - called shadowing - specify invocation of superclass method - keyword super - use like "this", only refers to inherited aspects of the object - useful when overriding method use overridden method functionality - by default, will refer to subclass method by that name 13.4 SUBCLASS CONSTRUCTION - invoke the superclass constructor - syntax: super(); - must be the first statement of the subclass constructor - if superclass constructor not called explicitly - implicitly called on default constructor - can get compiler error if all superclass constructors require parameters - subclass constructors commonly have parameters - some passed on to superclass constructor - some used to initialize subclass fields 13.5 CONVERTING BETWEEN SUBCLASS AND SUPERCLASS TYPES - convert a subclass to a superclass type - when a subclass extends a superclass, a subclass object is a special case of a superclass object - reference to subclass object can be converted to superclass reference - all references can be converted to type Object - multiple reference variables of different types may point to the same object - can only use methods of the type of the reference variable, even if the object is a subclass and has additional methods - motivation: reuse code that knows about the superclass but not the subclass - can use a subclass reference wherever a superclass is expected - convert from a superclass reference to a subclass reference - rare - use a cast - if wrong at runtime, causes an exception - instanceof operator - test whether an object belongs to a particular type - syntax: instanceof - returns true if is convertible to - could be - could be subclass of - use to make casts safe by making their execution dependent on instanceof 13.6 POLYMORPHISM - type of a variable doesn't completely depend on type of object to which it refers - variable with a given class as it's type can hold objects of that class or subclasses of that class - similar to interface variables - which method is called - always determined by the type of the actual object - not the type of the object reference - polymorphism: ability to refer to objects of multiple types with varying behavior - use more specific types for variables so compiler can check that only legal methods are invoked - method calls made without an explicit object - use the implicit parameter, this - what code is executed can still vary - this could refer to an object of the current class or a subclass 13.8 OBJECT: THE COSMIC SUPERCLASS - every class defined without an explicit extends clause automatically extends the class Object - Object is the direct or indirect superclass of every class in Java - useful Object methods - String toString() - returns a string representation of the object - boolean equals(Object otherObject) - tests whether the object equals another object - Object clone() - makes a full copy of an object - good idea to override these in your classes 13.8.1 OVERRIDING THE TOSTRING METHOD - returns a string represenation for each object - should describe the object state - used for debugging - called whenever you concatenate a string with an object - whenever one argument to the + operator is a string - compiler automatically invokes the toString method of the other to convert it to a string too - then concatentates the two strings - works because compiler knows every object has a toString method, because Object does, and every class inherits from Object - this process is different for primitives - they don't have any methods, much less the toString one - Object implementation - class name followed by the hash code - hash code used to tell objects apart: different objects likely to have different hash codes - better to override the object to expose state - recommended format - first print name of class - then values of the instance fields inside brackets - useful for debugging - simply print the object - println automatically calls toString to convert its argument to a string first AT 13.4 INHERITANCE AND THE TOSTRING METHOD - make toString method useable by subclasses - getClass method - returns a an object of the Class class - describes classes and their properties - getName method returns the name of the class - in subclass - override toString - add values of the subclass instance fields - call super.toString() to get superclass field values - use brackets to show which fields belong to the superclass