Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance.

Similar presentations


Presentation on theme: "David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance."— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance

2 7 October 2003CS 201J Fall 20032 Menu Quiz Results Subtyping Inheritance

3 7 October 2003CS 201J Fall 20033 Class Speed

4 7 October 2003CS 201J Fall 20034 Problem Set 5 Teams

5 7 October 2003CS 201J Fall 20035 Read PS3 Comments 6 of these also said the class is going too fast

6 7 October 2003CS 201J Fall 20036 Still in course if not required?

7 7 October 2003CS 201J Fall 20037 Sections

8 7 October 2003CS 201J Fall 20038 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

9 7 October 2003CS 201J Fall 20039 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!

10 7 October 2003CS 201J Fall 200310 Subtyping

11 7 October 2003CS 201J Fall 200311 Subtyping Cell ConwayLifeCell ConwayLifeCell is a subtype of Cell Cell is a supertype of ConwayLifeCell ConwayLifeCell ≤ Cell

12 7 October 2003CS 201J Fall 200312 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 ();

13 7 October 2003CS 201J Fall 200313 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?

14 7 October 2003CS 201J Fall 200314 Java’s Type Hierarchy Object String Cell ConwayLifeCell Object is the ultimate supertype of every object type.

15 7 October 2003CS 201J Fall 200315 Java 3D Class Hierarchy Diagram http://java.sun.com/products/java-media/3D/collateral/j3dclass.html RotationPathInterpolator PathInterpolator Interpolator Selector Node Leaf SceneGraphObject Not at all uncommon to have class hierarchies like this!

16 7 October 2003CS 201J Fall 200316 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…

17 7 October 2003CS 201J Fall 200317 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

18 7 October 2003CS 201J Fall 200318 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?


Download ppt "David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance."

Similar presentations


Ads by Google