180 likes | 347 Views
Lecture 11: Subtyping and Inheritance. David Evans http://www.cs.virginia.edu/evans. CS201j: Engineering Software University of Virginia Computer Science. Menu. Quiz Results Subtyping Inheritance. Class Speed. Problem Set 5 Teams. Read PS3 Comments. 6 of these also said the
E N D
Lecture 11: Subtyping and Inheritance David Evans http://www.cs.virginia.edu/evans CS201j: Engineering Software University of Virginia Computer Science
Menu • Quiz Results • Subtyping • Inheritance CS 201J Fall 2003
Class Speed CS 201J Fall 2003
Problem Set 5 Teams CS 201J Fall 2003
Read PS3 Comments 6 of these also said the class is going too fast CS 201J Fall 2003
Still in course if not required? CS 201J Fall 2003
Sections CS 201J Fall 2003
Comments 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 CS 201J Fall 2003
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! CS 201J Fall 2003
Subtyping CS 201J Fall 2003
Cell Subtyping ConwayLifeCell is a subtype of Cell Cell is a supertype of ConwayLifeCell ConwayLifeCell≤Cell ConwayLifeCell CS 201J Fall 2003
Subtype Substitution • If B is a subtype of A, everywhere the code expects an A, a B can be used instead • Examples: c1 must be a subtype of Cell (note A is a subtype of A) Cell c = c1; Cell c = new ConwayLifeCell (); ConwayLifeCell c = new Cell (); CS 201J Fall 2003
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? CS 201J Fall 2003
Java’s Type Hierarchy Object Cell String Object is the ultimate supertype of every object type. ConwayLifeCell CS 201J Fall 2003
RotationPathInterpolator PathInterpolator Interpolator Node Selector Leaf SceneGraphObject Not at all uncommon to have class hierarchies like this! Java 3D Class Hierarchy Diagram http://java.sun.com/products/java-media/3D/collateral/j3dclass.html CS 201J Fall 2003
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… CS 201J Fall 2003
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 CS 201J Fall 2003
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? CS 201J Fall 2003