Lecture 11 Subtyping and Inheritance CS 201 j
Lecture 11: Subtyping and Inheritance CS 201 j: Engineering Software University Virginia 7 October of 2003 Computer Science CS 201 J Fall 2003 David Evans http: //www. cs. virginia. edu/evans
Menu • Quiz Results • Subtyping • Inheritance 7 October 2003 CS 201 J Fall 2003 2
Class Speed 7 October 2003 CS 201 J Fall 2003 3
Problem Set 5 Teams 7 October 2003 CS 201 J Fall 2003 4
Read PS 3 Comments 6 of these also said the class is going too fast 7 October 2003 CS 201 J Fall 2003 5
Still in course if not required? 7 October 2003 CS 201 J Fall 2003 6
Sections 7 October 2003 CS 201 J Fall 2003 7
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 2003 CS 201 J Fall 2003 8
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 2003 CS 201 J Fall 2003 9
Subtyping 7 October 2003 CS 201 J Fall 2003 10
Subtyping Cell Conway. Life. Cell is a subtype of Cell is a supertype of Conway. Life. Cell ≤ Cell Conway. Life. Cell 7 October 2003 CS 201 J Fall 2003 11
Subtype Substitution • If B is a subtype of A, everywhere the code expects an A, a B can be used instead • Examples: Cell c = c 1; c 1 must be a subtype of Cell (note A is a subtype of A) Cell c = new Conway. Life. Cell (); Conway. Life. Cell c = new Cell (); 7 October 2003 CS 201 J Fall 2003 12
Subtype Examples java. util. Vector: public void add. Element (Object obj); public class String. Set { Vector elements; public void insert (String s) { elements. add. Element (s); } 7 October 2003 CS 201 J Fall 2003 Why we can use a String where an Object is expected? 13
Java’s Type Hierarchy Object Cell String Conway. Life. Cell 7 October 2003 Object is the ultimate supertype of every object type. CS 201 J Fall 2003 14
Node Path. Interpolator Selector Rotation. Path. Interpolator Leaf Scene. Graph. Object Not at all uncommon to have class hierarchies like this! Java 3 D Class Hierarchy Diagram 7 October 2003 CS 201 J Fall 2003 http: //java. sun. com/products/java-media/3 D/collateral/j 3 dclass. html 15
Subtype Examples java. util. Vector: public void add. Element (Object obj); public class Int. Set { Vector elements; Primitive types public void insert (int x) { are not subtypes elements. add. Element (x); of Object. elements. add. Element (new Integer (x)); } 7 October 2003 But Integer is… CS 201 J Fall 2003 16
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 7 October 2003 CS 201 J Fall 2003 both subtyping and inheritance just subtyping 17
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? 7 October 2003 CS 201 J Fall 2003 18
- Slides: 18