Object-Oriented Languages
• Classes How do we
access them?
– Types
– Collections of methods and fields
• Inheritance
I h it
– Allows methods and attributes declared in
one class to be copied into another.
Purdue University is an Equal Opportunity/Equal Access institution.
1
11/9/2009
Outline
• Single Inheritance
• Multiple Inheritance
• Testing Class Membership
• Private Fields and Methods
Outline
• Single Inheritance
• Multiple Inheritance
• Testing Class Membership
• Private Fields and Methods
2
11/9/2009
– await simulatin
– move g the
vehicles
Car Truck
• Truck is extended from class Car extends Vehicle{ class Truck extends Vehicle{
Vehicle int passengers;
void await(Vehicle v){
void move(int x){
if (x<=55) {
– position if (v.position < position)
v.move(position-v.position); }
position = position+x;
– move else
this.move(10); }
}
}
}
Purdue University is an Equal Opportunity/Equal Access institution.
Single Inheritance
• Single inheritance
– Each class extends a single base class
3
11/9/2009
Data Fields
• Prefixing (when B extends A)
Prefixing Example
class A { int a = 0; }
class B extends A { int b=0; int c=0; }
class C extends B { int d=0; }
c c
4
11/9/2009
Static Methods
• Methods declared static.
Example
• Variable c is an instance of class C.
• Class C extends class B. Not re-namable
5
11/9/2009
Dynamic Methods
class A { int x=0; int f() {…} }
This is incorrect!
class B extends A { int g() {…} } Offset for f is not
class C extends B { int g() {…} } constant.
class D extends C { int y=0; int f() {…} } Must pad space.
Object : x x x x
Class
y
descriptors
allocated in Descriptor : A B C D
static data x x x x
locations A_f A_f A_f y
6
11/9/2009
Outline
• Single Inheritance
• Multiple Inheritance
• Testing Class Membership
• Private Fields and Methods
Multiple Inheritance
• Problem
– If class C inherits from both class A and
class B, we cannot put both A’s fields and
B’s fields at the beginning of C at the
same time.
7
11/9/2009
A B C D
a b a a
c d b
• B x = new B c b
x.b load t1, M[x+4]
c
x = new D
x.b load t1, M[x+8] d
8
11/9/2009
A B C D
a a a
b b
c c
d d
Purdue University is an Equal Opportunity/Equal Access institution.
b b
• Model this problem as global
graph
g p coloring g c c
d d
•A node for each field name
•An interference edge between names that e
coexist in the same class (perhaps by
inheritance)
Purdue University is an Equal Opportunity/Equal Access institution.
9
11/9/2009
A B C D
a a a
b b
c c
empty slots in d d
each object e
Better Solution
Object Do the ggraph
p coloringg as
before. But now colors are the
offsets within the descriptors
Class Descriptor
rather than within the objects.
D
a b a
C a a:
c d
a: b b:
A B
a: a: c c:
b: empty space d d:
per class
d: e e:
Purdue University is an Equal Opportunity/Equal Access institution.
10
11/9/2009
Fetch a Field
object of class A
object of sub-class
sub class C
To fetch a field:
a a
• Fetch class descriptor
d • Fetch field-offset value from
C the class descriptor
a: 1
• Fetch the data
A
a: 1
d:
2
Purdue University is an Equal Opportunity/Equal Access institution.
Finding Methods
• Global Graph Coloring still works.
• Graph Nodes: method names plus the
field names.
• Descriptor entries give:
– For fields: locations within the objects.
– For methods: machine-code addresses of
the instances.
Purdue University is an Equal Opportunity/Equal Access institution.
11
11/9/2009
Overloaded Methods
• See textbook section 16.4
• Different functions of the same name
but different argument types.
• The compiler must choose between
function bodies based on the types of
the actual parameters.
Overloading (continue)
• Overloading is sometimes known as ad hoc polymorphism, as
opposed to the parametric polymorphism using constructs such
as templates in C++.
• Methods with the same name (in the same class) but different
types of parameters are implemented as different methods
• A convention is used to make the new method names, e.g. the
generic name + types of parameter
• p lists the beginning
The class descriptor g g addresses of all such
namesake methods
• When looking up f in a place where it is called with actual
parameters, the types of the actual parameters will determine
which of the implementations should be used.
12
11/9/2009
Visitor
PROGRAM 4
4.8:
8: An interpreter visitor
visitor.
Visitor (continue)
public class Interpreter implements Visitor {
public int visit(PlusExp n) {
return n.e1.accept(this)+n.e2.accept(this); }
public int visit(MinusExp n) {
return n.e1.accept(this)-n.e2.accept(this); }
public int visit(TimesExp n) {
return n.e1.accept(this)*n.e2.accept(this); }
public int visit(DivideExp n) {
return n.e1.accept(this)/n.e2.accept(this);
n e1 accept(this)/n e2 accept(this); }
public int visit(Identifier n) {
return lookup(n.f0); }
public int visit(IntegerLiteral n) {
return Integer.parseInt(n.f0); } }
13
11/9/2009
Visitor (continue)
PROGRAM 4
4.7:
7: Syntax classes with accept methods
Visitor (continue)
Note: The interface “Visitor”
Visitor is imported for compiling
these expression classes. Hence, the offsets of
different “visit” implementations are known to the
compiler.
14
11/9/2009
Visitor (continue)
Revisit the implementation:
The compiler does not know what subclasses are e1 and e2.
However, since accept() is not overloaded, and the same
offset is used to locate it in the class descriptor, looking for its
address is straightforward,
straightforward e e.g.
g
public class MinusExp extends Exp { public Exp e1,e2;
public MinusExp(Exp a1, Exp a2) { e1=a1;
e2=a2; }
public int accept(Visitor v) { return v.visit(this); }
}
Purdue University is an Equal Opportunity/Equal Access institution.
Outline
• Single Inheritance
• Multiple Inheritance
• Testing Class Membership
• Private Fields and Methods
15
11/9/2009
Variable x of class
C that points to (D)x
an object of class If we are
D that extends C certain it is an
instance of C
Purdue University is an Equal Opportunity/Equal Access institution.
16
11/9/2009
Instanceof Example
• For single inheritance, x instanceof C:
t1 ← x.descriptor
L1: if t1 = C goto true
t1 ← t1.super
if t1 = nil goto false
goto L1
Purdue University is an Equal Opportunity/Equal Access institution.
C
1st iteration: d.descriptor != C
2nd iteration: d.super.descriptor == C
return true;
Purdue University is an Equal Opportunity/Equal Access institution.
17
11/9/2009
Type Coercion
• Given a variable c of type C, we can
treat c as any supertype B of C.
Outline
• Single Inheritance
• Multiple Inheritance
• Testing Class Membership
• Private Fields and Methods
18
11/9/2009
Enforce Privacy
• Add a boolean flag to each field or
method in the symbol table of a class C.
19
11/9/2009
20