1 COS 260 DAY 18 Tony Gauvin
2 Agenda Questions? 7 th Mini quiz Graded –Good results 8 th Mini Quiz –Chap 8 Next class Assignment 4 Due Assignment 5 Posted –Due November 19 Capstone progress reports over due –Next progress report due November 12 –Timing of proposal and reports is 10% of project grade Finish Discussion on Improving Structure with Inheritance
Improving structure with inheritance 5.0
4 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
5 Review (so far) Inheritance (so far) helps with: Avoiding code duplication Code reuse Easier maintenance Extendibility Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
6 Define Inheritance Superclass Subclass Hierarchy Subtype Parent class Child class Is-a Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
7 Subclasses and subtyping Classes define types. Subclasses define subtypes. Objects of subclasses can be used where objects of supertypes are required. (This is called substitution.) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
8 Subtyping and assignment Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Vehicle v1 = new Vehicle();Vehicle v2 = new Car();Vehicle v3 = new Bicycle(); subclass objects may be assigned to superclass variables
9 Invalid Subtyping Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Car c1 = new Vehicle(); Car c2 = new Bicycles(); These are errors c1 & c2 must be a type that is a Superclass of the assigned object
10 Subtyping and parameter passing Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class NewsFeed { public void addPost(Post post) {... } PhotoPost photo = new PhotoPost(...); MessagePost message = new MessagePost(...); feed.addPost(photo); //photo subtype of Post feed.addPost(message); //message subtype of Post subclass objects may be used as actual parameters for the superclass
11 Object diagram Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
12 Class diagram Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
13 Polymorphic variables Object variables in Java are polymorphic. (They can hold objects of more than one type.) They can hold objects of the declared type, or of subtypes of the declared type. Polymorphic means “many forms”. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
14 Casting We can assign subtype to supertype … … but we cannot assign supertype to subtype! Vehicle v; Car c = new Car(); v = c; // correct c = v; // compile-time error! Casting fixes this : c = (Car) v; (only ok if the vehicle really is a Car!) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
15 Casting An object type in parentheses. Used to overcome 'type loss'. The object is not changed in any way. A runtime check is made to ensure the object really is of that type: –ClassCastException if it isn't! Use it sparingly. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
16 The Object class Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling All classes inherit from Object.
17 The Object Class cs/api/java/lang/Object.html cs/api/java/lang/Object.html clone() equals(object obj) finalize() getClass() toString() wait().. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
18 Polymorphic collections All collections in JAVA are polymorphic. The elements could simply be of type Object. public void add(Object element) public Object get(int index) Usually avoided by using a type parameter with the collection. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
19 Polymorphic collections A type parameter limits the degree of polymorphism: ArrayList Collection methods are then typed. Without a type parameter, ArrayList is implied. Likely to get an “unchecked or unsafe operations” warning. More likely to have to use casts. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
20 Collections and primitive types Potentially, all objects can be entered into collections because collections can accept elements of type Object and all classes are subtypes of Object. Great! But what about the primitive types: int, boolean, etc.? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
21 Wrapper classes Primitive types are not objects types. Primitive-type values must be wrapped in objects to be stored in a collection! Wrapper classes exist for all primitive types: Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling simple typewrapper class intInteger floatFloat charCharacter...
22 Wrapper classes Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling int i = 18; Integer iwrap = new Integer(i); … int value = iwrap.intValue(); wrap the value unwrap it In practice, autoboxing and unboxing mean we don't often have to do this explicitly
23 Autoboxing and unboxing Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling private ArrayList markList; … public void storeMark(int mark) { markList.add(mark); } int firstMark = markList.remove(0); autoboxing unboxing
24 Review Inheritance allows the definition of classes as extensions of other classes. Inheritance –avoids code duplication –allows code reuse –simplifies the code –simplifies maintenance and extending Variables can hold subtype objects. Subtypes can be used wherever supertype objects are expected (substitution). Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling