1 / 18

David Evans cs.virginia/evans

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

xia
Télécharger la présentation

David Evans cs.virginia/evans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Menu • Quiz Results • Subtyping • Inheritance CS 201J Fall 2003

  3. Class Speed CS 201J Fall 2003

  4. Problem Set 5 Teams CS 201J Fall 2003

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

  6. Still in course if not required? CS 201J Fall 2003

  7. Sections CS 201J Fall 2003

  8. 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

  9. 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

  10. Subtyping CS 201J Fall 2003

  11. Cell Subtyping ConwayLifeCell is a subtype of Cell Cell is a supertype of ConwayLifeCell ConwayLifeCell≤Cell ConwayLifeCell CS 201J Fall 2003

  12. 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

  13. 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

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

  15. 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

  16. 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

  17. 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

  18. 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

More Related