Você está na página 1de 20

11/9/2009

Department of Computer Sciences

Object-Oriented Languages

For CS352 Fall of 2009

Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

Overview of Object-oriented Language

• 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

Department of Computer Sciences

Outline
• Single Inheritance
• Multiple Inheritance
• Testing Class Membership
• Private Fields and Methods

Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

Outline
• Single Inheritance
• Multiple Inheritance
• Testing Class Membership
• Private Fields and Methods

Purdue University is an Equal Opportunity/Equal Access institution.

2
11/9/2009

Department of Computer Sciences

What is Class Extension?


• Car is extended from Imagine
Vehicle a main() class Vehicle {
int position;
method void move(int x) {
– passenger Vehicle postion = position + x;
executin }
– position; g a loop
}

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

Department of Computer Sciences

Single Inheritance
• Single inheritance
– Each class extends a single base class

• Compiler issues Where to allocate code?


Where to allocate data?
– Data fields How to find code and data
at run time?
– Static methods consider the car example
Class versus object
– Dynamic methods
Purdue University is an Equal Opportunity/Equal Access institution.

3
11/9/2009

Department of Computer Sciences

Data Fields
• Prefixing (when B extends A)

– Those fields of B that are inherited from A


are placed at the beginning in a B record.

– Those not inherited from A are placed


afterward.
Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

Prefixing Example
class A { int a = 0; }
class B extends A { int b=0; int c=0; }
class C extends B { int d=0; }

Make sure offset of


A B C each variable of the
same name is
a a a
constant
b b

c c

Purdue University is an Equal Opportunity/Equal Access institution. d

4
11/9/2009

Department of Computer Sciences

Static Methods
• Methods declared static.

• To compile a static method, the


compiler will try to find out the definition
of this method in the ancestor class.
class

Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

Example
• Variable c is an instance of class C.
• Class C extends class B. Not re-namable

• Class B extends class A.


• Method f is defined as a static method in A.

• When c.f() is called, it will be compiled to


label A_f. (In assembly code, jump to an
address)
Purdue University is an Equal Opportunity/Equal Access institution.

5
11/9/2009

Department of Computer Sciences

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

B_g C_g D_f


e.g., A_f indicates method f
defined in class A. C_g

Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

Execute Dynamic Methods


• To execute c.f() Example
1. Fetch the class descriptor Object :
d from object c x

2. Fetch the method-


instance pointer p from
Descriptor : C
the (constant) f offset of d Pointer to
x
3
3. C ll p
Call A_f
machine code
for method f
C_g

Purdue University is an Equal Opportunity/Equal Access institution.

6
11/9/2009

Department of Computer Sciences

Outline
• Single Inheritance
• Multiple Inheritance
• Testing Class Membership
• Private Fields and Methods

Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

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.

Purdue University is an Equal Opportunity/Equal Access institution.

7
11/9/2009

Department of Computer Sciences

Multiple Inheritance Cont.


class A { var a := 0 }
class B { var b := 0 ; var c := 0 }
class C extends A { var d := 0 }
class D extends A,B,C { var e := 0 }

A B C D

a b a a

c d b

This layout doesn’t work because c


b,c,d will have different offsets in
sub classes. d
Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

Why It Does Not Work?


• OO Property: If D is a subclass of
B, then an object of class D can B D
be used wherever an object of
class B is expected b a

• 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

• But the same code should work e


unmodified for both class B and
class D
Purdue University is an Equal Opportunity/Equal Access institution.

8
11/9/2009

Department of Computer Sciences

Global Graph Coloring


class
l A { var a := 0 }
class B { var b := 0; var c := 0 }
class C extends A { var d := 0 }
class D extends A,B,C { var e := 0 }

A B C D

a a a

b b

c c

d d
Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

Global Graph Coloring Cont.


• Statically
St ti ll analyze
l allll classes.
l
A B C D
• Find some offset that can be used
everywhere for each field name. a a a

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

Department of Computer Sciences

Problem with the Above Approach

A B C D

a a a

b b

c c

empty slots in d d
each object e

Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

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

Department of Computer Sciences

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.

Department of Computer Sciences

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

Department of Computer Sciences

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.

Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

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.

Purdue University is an Equal Opportunity/Equal Access institution.

12
11/9/2009

Department of Computer Sciences

Visitor
PROGRAM 4
4.8:
8: An interpreter visitor
visitor.

public interface Visitor {


public int visit(PlusExp n);
public int visit(MinusExp n);
public int visit(TimesExp n);
public int visit(DivideExp n);
public int visit(Identifier n);
public int visit(IntegerLiteral n); }

A class that implements this interface will have 6 “visit”


methods (with 6 different addresses) entered in the class
descriptor.

Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

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); } }

Purdue University is an Equal Opportunity/Equal Access institution.

13
11/9/2009

Department of Computer Sciences

Visitor (continue)
PROGRAM 4
4.7:
7: Syntax classes with accept methods

public abstract class Exp {


public abstract int accept(Visitor v); }

public class PlusExp extends Exp { public Exp e1,e2;


public PlusExp(Exp a1, Exp a2) { e1=a1; e2=a2; }
public int accept(Visitor v) { return v.visit(this); } }

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.

Department of Computer Sciences

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.

When compiling public class PlusExp, when the


compiler sees

“public int accept(Visitor v) { return v.visit(this); } }”, it


knows “this” is for PlusExp, and hence should use the
offset for visit(PlusExp) to find the starting address of
“v.visit”.

v.visit(this) will cause visit(PlusExp) to execute

Purdue University is an Equal Opportunity/Equal Access institution.

14
11/9/2009

Department of Computer Sciences

Visitor (continue)
Revisit the implementation:

public int visit(PlusExp n) {


return n.e1.accept(this)+n.e2.accept(this); }

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.

Department of Computer Sciences

Outline
• Single Inheritance
• Multiple Inheritance
• Testing Class Membership
• Private Fields and Methods

Purdue University is an Equal Opportunity/Equal Access institution.

15
11/9/2009

Department of Computer Sciences

Test Membership of An Object


• Some object-oriented languages allow
the program to test membership of an
object in a class at run time
• Each object points to its class descriptor
• Address of class descriptor can be
“type-tag”
• Need to think about inheritance
Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

Testing Class Membership Examples


Test whether object Java
x belongs to x instanceof C
class C or any
subclass of C

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

Department of Computer Sciences

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.

Department of Computer Sciences

Instanceof Example (cont.)


d.descriptor
class D extends class C
D
d ← new D
Test whether d instanceof C d.super.descriptor

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

Department of Computer Sciences

Type Coercion
• Given a variable c of type C, we can
treat c as any supertype B of C.

• A variable b of type B might or might not


be an instance of C at run time,
assignment c ← b might cause
problems.
Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

Outline
• Single Inheritance
• Multiple Inheritance
• Testing Class Membership
• Private Fields and Methods

Purdue University is an Equal Opportunity/Equal Access institution.

18
11/9/2009

Department of Computer Sciences

Private Fields and Methods


• Protects fields of objects from direct
manipulation by other methods.

– Private fields cannot be accessed outside


the object.

– Private methods cannot be called from


outside.
Purdue University is an Equal Opportunity/Equal Access institution.

Department of Computer Sciences

Enforce Privacy
• Add a boolean flag to each field or
method in the symbol table of a class C.

• When compiling, do a simple check.

Purdue University is an Equal Opportunity/Equal Access institution.

19
11/9/2009

Department of Computer Sciences

Varieties of Privacy and Protection


• Fields and methods which are
– accessible to the class that declares them
– accessible to the declaring class, and any
subclasses (protected in C++)
– accessible within the same package or
namespace

• Static type checking (CS565)


Purdue University is an Equal Opportunity/Equal Access institution.

20

Você também pode gostar