0% found this document useful (0 votes)
11 views38 pages

Oo 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views38 pages

Oo 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

CS571: Programming Languages

OO Languages II
(a.k.a. Under the Hood of Java/C++)

CS571 Programming Languages 1


Java Interface

2
Interface
An interface may only contain methods with
empty bodies and constant declarations
(variables declarations which are declared to be
both static and final).
e.g. A bicycle's behavior:
interface Bicycle
{ void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}

3
Interface
Interface cannot be instantiated – they can only be
implemented by classes or extended by other
interfaces.

A (non-abstract) class that implements an interface


must implement all of the interface's methods.
It is possible, however, to define a class that does
not implement all of the interface methods,
provided that the class is declared to be abstract.
abstract class X implements Y
{ // implements all but one method of Y }
class XX extends X
{ // implements the remaining method in Y }
4
Interface and Subtyping
The class implementing the interface is a
subtype of the interface type

5
Subtyping: A Summary

Who defines the subtype relationship? The


programmers!
 Java: A is a subtype of B iff
 (1) A is a subcass of B; OR

 (2) A implements B as an interface.

 C++: A* is a subtype of B* iff A is a subclass of B


AND the inheritance is public
 Subtyping is reflexive
 Subtyping is transitive
Who uses the subtyping relationship? The
Compiler in its typechecking process
 Subtype principle: if I expect a value of a type, it is
ok the value at runtime is a subtype of that type.
6
Subtype Polymorphism

7
Subtype Polymorphism

The same piece of code can be reused by taking


different types of objects (as real method
arguments), as long as they all belong to
subtypes of a base type.

8
Static vs. Dynamic Binding/Dispatch

Given an expression o.m(v), should the


compiler decide which m is invoked (static
binding/dispatch), or the run-time decide
which m is invoked (dynamic dispatch)?
 C++: dynamic binding is an option, but is not
the default.
 Virtual function: dynamic

 Non-virtual: static

 Java: all methods are implicitly virtual, i.e.,


dynamic binding always applies.

9
Dynamic Binding (Java)
class A Output:
{ void p() {System.out.println(“A.p”);}
void q() {System.out.println(“A.q”);} A.p
void f() {p(); q();} A.q
}
B.p
class B extends A
{ void p() {System.out.println(“B.p”);} B.q
void q() {System.out.println(“B.q”); super.q();} A.q
void f() {p(); q();}
}
public class Main{
public static void main(String[] args)
{A a = new A(); a.f();
a = new B(); a.f();
}
}
10
Example: Virtual Function (C++)
int main()
class A { A* a = new A();
{ public: a->f();
a = new B();
virtual void p() {cout << “A.p\n”;} a->f();
}
virtual void q() {cout << “A.q\n”;}
virtual void f() { p(); q();}
};
Same Output
class B : public A
(except no super call)
{ public:
void p() { cout << “B.p\n”;}
void q() { cout << “B.q\n”;}
void f() { p(); q();}
};

11
Virtual Function

Once Virtual, always virtual


 Once a base-class defines a function as

virtual, it remains virtual through out the


inheritance hierarchy.

Costs 10% to 20% extra overhead compared


to a non-virtual function call.

12
Example: Virtual Function (C++)
class A{ int main(){
A* a = new D();
public: void p(){ std::cout<< "A.p\n";}
a->p();
}; C* c = new D();
c->p();
}
class B: public A{
public: virtual void p(){ std::cout<< "B.p\n";}
};

class C: public B{
public: void p(){ std::cout<< "C.p\n"; }
};
Result:
class D : public C{ A.p
public: void p(){ std::cout<< "D.p\n";}
}; D.p

13
Example: Virtual Function (C++)
int main()
class A { A* a = new A;

{ public: B* b = new B;
a -> f(); b -> f();
void p() {cout << “A.p\n”;} a = b; a -> f();
}
virtual void q() {cout << “A.q\n”;}
void f() { p(); q();}
};
class B : public A
Result:
{ public:
A.p
void p() { cout << “B.p\n”;}
A.q
void q() { cout << “B.q\n”;}
A.p
};
B.q
A.p
B.q
14
Example: Virtual Function (C++)
int main()
class A { A* a = new A;
{ public: B* b = new B;
a -> f(); b -> f();
void p() {cout << “A.p\n”;} a = b; a -> f();
}
virtual void q() {cout << “A.q\n”;}
void f() { p(); q();}
};
class B : public A
Result:
{ public:
A.p
void p() { cout << “B.p\n”;}
A.q
void q() { cout << “B.q\n”;}
void f() { p(); q();} B.p
}; B.q
A.p
B.q
15
Example: C++ Object vs. Object
Pointers
class A int main()
{ A a; B b;
{ public: a.f(); b.f();
a = b; a.f();
void p() {cout << “A.p\n”;} }
void q() {cout << “A.q\n”;}
void f() { p(); q();}
};
Result:
class B : public A
A.p
{ public:
A.q
void p() { cout << “B.p\n”;}
A.p
void q() { cout << “B.q\n”;}
A.q
};
A.p
A.q

16
Example: C++ Object vs. Object
Pointers
class A int main()
{ A a; B b;
{ public: a.f(); b.f();
a = b; a.f();
void p() {cout << “A.p\n”;} }
virtual void q() {cout << “A.q\n”;}
void f() { p(); q();}
};
Result:
class B : public A
A.p
{ public:
A.q
void p() { cout << “B.p\n”;}
A.p
void q() { cout << “B.q\n”;}
B.q
};
A.p
A.q

17
Implementation of Virtual Function
(C++)

Non-virtual function: in the implementation of


objects, allocation (other than the memory for
the code itself) of non-virtual functions is not
needed – the location of such functions are
known at compile time, as is the case for
ordinary functions in an imperative language.

18
Implementation of Virtual Function
(C++)

Virtual Function: the method to use for a call


is not known except during execution
 Solution 1
 Treat function members like data members.

 Allocate storage for them within the object.

 Store pointers in the object which point to

virtual functions.

19
Implementation of Virtual Function
(C++)

Solution 1:

class A:{
public:
double x; Space for x
void f() {…}; Ptr to g
virtual void g() {…};
};
class B: public A{
Space for x
public:
double z; Ptr to g
void f() {…}; Space for z
virtual void g() {…};
};

20
Implementation of Virtual Function
(C++)

Disadvantage of solution 1
 Each object structure must maintain a list of
all virtual functions available at that moment
– the list could be very large

Solution 2
 Maintain a table of virtual methods for each
class at some central location that can be
statically loaded and have a single pointer to
this table stored in each object structure
 Such a table is called a virtual method table
(VMT)

21
Implementation of Virtual Function
(C++)
class B {
int i; b1:

char c; VMT prt


virtual void g();
space for i
virtual void h(); Virtual Method Table(VMT)
space for c
} for class B
B b1, b2; Ptr to B’s g
b2:
VMT prt Ptr to B’s h
space for i
space for c

22
Implementation of Virtual Function
(C++)

class A:{
VMT for class A
public:
double x; VMT prt Ptr to A’s g
void f() {…}; Space for x
virtual void g() {…};
};
class B: public A{ VMT for class B
public:
VMT prt Ptr to B’s g
double z;
void f() {…}; space for x
virtual void g() {…};space for z
};

23
Field Allocation

24
Field Allocation
Objects, with their fields and virtual table
pointers, are allocated on the heap.
The layout for a field is the same as it is done
for structures in C or other languages.
 The fields are allocated next to each other.
 Some padding may be required in between
fields.
 Statically translate field names into relative
addresses, with respect to the beginning of the
object.
 Fields for a subclass immediately follow the
fields of the superclass.

25
Field Allocation
Fields are allocated next to each other
class Point {
private double x, y;
}
 An object of Point could be allocated space for
its instance variables x and y just as the
structure

struct
{ double x; space for x
double y; space for y
};

26
Padding & Relative Addressing

class B{
int i;
double d;
}
Layout of objects of type B:

//assume int need 4 bytes


0: int i
//pad, assuming doubles are aligned on 8-byte boundaries
4: xxxxxxxxxx
//assume double need 8 bytes
8: double d

27
Inheritance of Fields
e.g. in Java:

class Point{ class ColoredPoint extends Point{


public double x,y; public Color color;
} }

Space for x Point part Space for x


Space for y
Space for y
ColoredPoint part Space for color

The instance variables x and y inherited by any


ColorPoint object from Point object can be found
at the same offset from the beginning of its
allocated space as for any point object

28
Casting

29
Casting: Data Conversion and Type
Coercion
The ubiquitous (T) e expression is available
in both C++ and Java, but their meanings
are wildly different:
 In C++, it means data conversion, i.e.
converting the runtime layout of e to conform
to type T. This is to follow the C tradition.
 In Java, it means type coercion, i.e. inserting a
runtime check to make sure e has type T or a
subtype of T. Expression (T)e does have type
T for the type checker.
 The type coercion version of C++ is
dynamic_cast.

30
Upcasting vs. Downcasting
Upcasting: casting an object to a more
general type - always legal in both C++ and
Java.
Downcasting: casting an object to a more
specific type - Java inserts a run-time check
to ensure that the cast is legal.

31
The Essence of Casting
Compile time: trust
Run time: verify

32
Downcasting (Java)
A a;
B b;
a = new A();
b = a;

33
Downcasting (Java)
A a;
B b;
a = new A();
b = a; // a compile-time error in Java

34
Downcasting (Java)
A a;
B b;
a = new A();
b = a; // a compile-time error in Java
b = (B) a; // now no compiler error – downcasting
// java.lang.ClassCastException

35
Downcasting (Java)
A a;
B b;
a = new B();
b = (B) a; //no compile or run-time error

36
My Favorite Question

Given a Java program:


class Point {
protected int x;
protected int y;
}

class ColorPoint extends Point {


protected int color;
public int getColor() {return color;}
}

public class test {


public static void main (String[] args){
Point p1 = new Point(); //line1
ColorPoint p2 = (ColorPoint)p1; //line2
int temp = p2.getColor(); //line3
}
}

37
My Favorite Question
Which of the following claims is correct?

(a) This program does not compile, since type checking on


downcasting would fail.

(b) This program compiles, and run-time error would not


appear until line2 gets executed, since downcasting would
fail.

(c) This program compiles, and run-time error would not


appear until line3 gets executed, since variable p2 does not
have a getColor method.

(d) This program compiles, and no run-time error would


happen. line3 would return the default value of the color
field, 0.
38

You might also like