Oo 2
Oo 2
OO Languages II
(a.k.a. Under the Hood of Java/C++)
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.
5
Subtyping: A Summary
7
Subtype Polymorphism
8
Static vs. Dynamic Binding/Dispatch
Non-virtual: static
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
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++)
18
Implementation of Virtual Function
(C++)
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:
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:
27
Inheritance of Fields
e.g. in Java:
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
37
My Favorite Question
Which of the following claims is correct?