David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance
7 October 2003CS 201J Fall Menu Quiz Results Subtyping Inheritance
7 October 2003CS 201J Fall Class Speed
7 October 2003CS 201J Fall Problem Set 5 Teams
7 October 2003CS 201J Fall Read PS3 Comments 6 of these also said the class is going too fast
7 October 2003CS 201J Fall Still in course if not required?
7 October 2003CS 201J Fall Sections
7 October 2003CS 201J Fall Comments “I’m still not convinced why we focus more time on programming concepts (invariants, requires, etc…) instead of code. I’d rather see problem sets with more implementation of code and less writing/methodical work. I just feel there is so much to Java we could be implementing and learning.” Why not focus more on learning Java? - Some of you will be professional programmers Will need to program in lots of other languages Will need to be better than average programmers - Some of you won’t want to program again after this class Concepts apply to more than just programming: whatever you design will benefit from abstraction, invariants, specifications, notions of subtyping/inheritance, understanding concurrency, etc. - As students at a liberal arts institution, you should be learning intellectually valuable things, not temporary skills
7 October 2003CS 201J Fall Comments Spend more time on Java syntax, need to cover Java more, etc. –Two weeks ago you were asked, “send any questions you have about Java programming” –Only 2 people sent any questions More TAs –Only 2-3 people at each of the lab hours Sunday and Monday!
7 October 2003CS 201J Fall Subtyping
7 October 2003CS 201J Fall Subtyping Cell ConwayLifeCell ConwayLifeCell is a subtype of Cell Cell is a supertype of ConwayLifeCell ConwayLifeCell ≤ Cell
7 October 2003CS 201J Fall Subtype Substitution If B is a subtype of A, everywhere the code expects an A, a B can be used instead Examples: Cell c = c1; c1 must be a subtype of Cell (note A is a subtype of A) Cell c = new ConwayLifeCell (); ConwayLifeCell c = new Cell ();
7 October 2003CS 201J Fall Subtype Examples java.util.Vector: public void addElement (Object obj); public class StringSet { Vector elements; public void insert (String s) { elements.addElement (s); } Why we can use a String where an Object is expected?
7 October 2003CS 201J Fall Java’s Type Hierarchy Object String Cell ConwayLifeCell Object is the ultimate supertype of every object type.
7 October 2003CS 201J Fall Java 3D Class Hierarchy Diagram RotationPathInterpolator PathInterpolator Interpolator Selector Node Leaf SceneGraphObject Not at all uncommon to have class hierarchies like this!
7 October 2003CS 201J Fall Subtype Examples java.util.Vector: public void addElement (Object obj); public class IntSet { Vector elements; public void insert (int x) { elements.addElement (x); } Primitive types are not subtypes of Object. elements.addElement (new Integer (x)); But Integer is…
7 October 2003CS 201J Fall Inheritance To implement a subtype, it is often useful to use the implementation of its supertype This is also called “subclassing” In Java: class B extends A B is a subtype of A B inherits from A class C implements F C is a subtype of F both subtyping and inheritance just subtyping
7 October 2003CS 201J Fall Charge Subtyping –Allow one type to be used where another type is expected Inheritance –Reuse implementation of the supertype to implement a subtype Thursday: –When is it safe to say B is a subtype of A?