Lecture 11 - University of Virginia, Department of Computer Science

Download Report

Transcript Lecture 11 - University of Virginia, Department of Computer Science

Lecture 11:
Subtyping and
Inheritance
CS201j: Engineering Software
University of Virginia
Computer Science
David Evans
http://www.cs.virginia.edu/evans
Menu
• Quiz Results
• Subtyping
• Inheritance
7 October 2003
CS 201J Fall 2003
2
Class Speed
20
18
16
14
12
10
8
6
4
2
0
Way too fast
7 October 2003
Too Fast
Just about right
CS 201J Fall 2003
Too Slow
3
Problem Set 5 Teams
20
18
16
14
12
10
8
6
4
2
0
Assigned
7 October 2003
Pick
CS 201J Fall 2003
Option
4
Read PS3 Comments
18
16
14
12
10
8
6 of these
also said the
class is going
too fast
6
4
2
0
Yes
7 October 2003
Skimmed
Looked at a few
CS 201J Fall 2003
No, but will
before Exam 1
No, just killing
trees
5
Still in course if not required?
35
30
25
20
15
10
5
0
No
7 October 2003
Yes
CS 201J Fall 2003
Not Required
6
30
25
Sections
20
15
10
5
0
Worthwhile
Not Worthwhile
I rarely go
30
25
20
15
10
5
0
More working through More working in groups
problems on the
chalkboard
7 October 2003
CS 201J Fall 2003
Working in lab on
problem sets
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 201J 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 201J Fall 2003
9
Subtyping
7 October 2003
CS 201J Fall 2003
10
Subtyping
Cell
ConwayLifeCell is a subtype of Cell
Cell is a supertype of ConwayLifeCell
ConwayLifeCell ≤ Cell
ConwayLifeCell
7 October 2003
CS 201J 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 = 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 2003
CS 201J Fall 2003
12
Subtype Examples
java.util.Vector:
public void addElement (Object obj);
public class StringSet {
Vector elements;
public void insert (String s) {
elements.addElement (s);
}
7 October 2003
CS 201J Fall 2003
Why we can use
a String where
an Object is
expected?
13
Java’s Type Hierarchy
Object
Cell
String
ConwayLifeCell
7 October 2003
Object is the ultimate supertype of
every object type.
CS 201J Fall 2003
14
Node
PathInterpolator
Interpolator
Selector
RotationPathInterpolator
Leaf
SceneGraphObject
Not at all uncommon to have
class hierarchies like this!
Java
3D Class
Hierarchy Diagram
7 October
2003
CS 201J Fall 2003
http://java.sun.com/products/java-media/3D/collateral/j3dclass.html
15
Subtype Examples
java.util.Vector:
public void addElement (Object obj);
public class IntSet {
Vector elements;
Primitive types
public void insert (int x) {
are not subtypes
elements.addElement (x); of Object.
elements.addElement (new Integer (x));
}
7 October 2003
But Integer is…
CS 201J 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 201J 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 201J Fall 2003
18