Title: Object Oriented Programming
1Object Oriented Programming
- Encapsulation
- Data and the code to manipulate it are defined
together - Enables data hiding and modularity
- Inheritance
- Mechanism for adding detail to a definition
- Enables code reuse and organization
- Models the world in an intuitive fashion
- A class file can extend another (parent-child
relationship) - Polymorphism
- Multiple methods with the same name
- Code reuse (same caller code for multiple methods)
2Object-based Programming Objects w/out
inheritance
- Code Reuse
- Methods dont rewrite the same insts multiple
times - Polymorphism
- Overloaded methods same method name, different
parameter lists - We could get both of these without objects in
this case, objects are merely organizing the
codes. C-based libraries give you the same
advantages.
3Variables
- class variables - one per class
- Declared static in Java, C
- C - declaration is separate from initialization
- Accessed using references variable name or class
name - Class name suggested for clarity
- instance variables one per object
- Always accessed using references variable name
4Methods
- class methods associated with a class
- Declared static in Java, C
- Accessed using references variable name or class
name - May only access class data since no instance
variable - instance methods associated with an object
- Always accessed using references variable name
- Intended to operate on instance data
- If not, method should be a class method
5Example Static vs Instance Variables
Class MyClass int foo static int bar static
void stat_method() foo 1
inst_method() bar 1 void
inst_method() bar 1
6Object-Based implementation
- Instance variables stored in each object
- Each field is a constant offset from beginning of
object - One set of class variables
- All methods are determined at compile time
static binding - Instance methods pass in this pointer as hidden
first argument to allow access to instance data
7Object-Based Implementation (No Inheritance)
Internal Type Representation
Type Ref
Internal Class Representation
Other
Field 1
Internal data structure for static members
Field 2
static field 1
. . .
static field 2
Object instances of same class
static method 1
inst method 1
Class variables
. . .
Instance variables
- Method executable code bodies
8OOP - Inheritance
- Code Reuse
- Extending objects rather than implementing from
scratch - Same code can be used to operate on objects of
different types (as long as they are descendants
of the same type) - Polymorphism
- Dynamic Dispatch allows the same code to call a
different method depending on the type of the
object. - The implementation determines what is allowed.
- Statically, requires that if a reference is of a
certain type, all of its methods are guaranteed
to be implemented in the type of the object to
which it points. - Dynamic dispatch of some methods allows us to
wait until run-time to need to know the type of
the object.
9Another polymorphism example
int x,y void draw() void moveTo()
- class GraphicalObject protected int x,
y public void draw() public void
moveTo(int x, int y) this.x x this.y
y - class Line extends GraphicalObject int k
- public void draw()
-
GraphicalObject
GraphicalObject
Line
10Compile errors
- GraphicalObject obj // Base class (Parent class)
- Line line // Derived class (Child class)
-
- obj line // ok
- line obj // not ok -- why?
11Binding instance methods of reference types
- Dynamic binding virtual methods
- At run-time, the dynamic type (type of object),
not reference type, is used to determine the
method to call - Virtual methods override the parents method
- This is the default for Java
- Only works on references.
- Static binding
- At compile-time, the static type (type declared
by reference) is used to determine the method
call - This is the default for C
- This is not polymorphism
- C - dynamic and static binding of instance
methods - Java only dynamic binding
12We need to augment implementation
- Reuse
- Make a larger object with data from all ancestors
- Polymorphism
- Do not know what method is going to be called
until run-time - Dynamic binding for virtual methods
13OOP Implementation (Single Inheritance) - data
- In object, there is a header
- In header is a pointer to the internal type
representation - This internal type representation holds
- The table of virtual methods
- Order methods from highest parent in hierarchy
first - Overridden methods, replace parent entry
- A pointer to the internal representation of the
class (class structure) - Also in the object are storage for the instance
fields - Order fields from highest parent in hierarchy
first - Fields are not overridden, they are just
shadowed, so all are in object
Internal Type Representation
Type Ref
Internal Class Representation
Other
VM1
Field 1
VM2
Field 2
VMT
VM3
. . .
. . .
Object instances of same class
14OOP Implementation - methods
Internal Type Representation
Type Ref
Internal Class Representation
Other
VM1
Field 1
VM2
Field 2
VMT
VM3
. . .
. . .
Object instances of same class
Internal data structure for static members
- Method executable code bodies
static field 1
static field 2
Internal class representation Locations
(offsets) of instance fields and methods as
well as static fields and methods
static method 1
. . .
15Method Binding/Dispatch
- Static
- Compiler finds/inserts address of a called method
into call - By looking up static type of the object (if an
instance method) - All non-virtual instance methods in C
- All static (class) methods in Java and C
- All methods in C
- Dynamic
- Compiler inserts code to look up the method at
runtime and finding it based on the underlying
object type - All instance methods in Java
- Virtual instance methods in C
16An Example
- GraphicalObject obj // a local variable
- Line line // another variable
-
- obj getAGraphicalObject()
- line getALine ()
- line.moveTo(0, 0) // inheritance at work
- obj.draw() //what is executed??
- line.draw() // and here?
- obj line
- obj.draw() // how about here?
- // What would execute with static binding?
17Which is called?
//(C) class Parent public
void test1() // no virtual keyword
P("in Parent's testit\n")
static void test2() P("in
Parent's static testit\n") int
main() Parent p1 new Child()
p1-gttest1() p1-gttest2()
18Virtual vs not virtual which is called?
//(C) class Parent class Child public
Parent public public void
testit() void testit() P("in
Parent's testit\n") P("in Child's
testit\n") virtual void
vtestit() void vtestit()
P( "in Parent's vtestit\n") P("in
Child's vtestit\n")
int main() Parent p new
Parent() Child c new Child() p-gttestit()
p-gtvtestit() // if object type matches ref
type, c-gttestit() c-gtvtestit() // there is no
question p c // polymorphism p-gttestit()
p-gtvtestit() // which gets called?
19C Virtual v/s Non-Virtual Methods
//(C) class Parent class Child public
Parent public public void
testit() void testit() P("in
Parent's testit\n") P("in Child's
testit\n") virtual void
vtestit() void vtestit()
P( "in Parent's vtestit\n")
P("in Child's vtestit\n")
testit() testit() void
test2() void test2() P(in
Parents test2\n) P(in Childs test2\n)
vtestit() vtestit()
int main() p c //polymorphic var
(p) p-gttestit() p-gtvtestit() p-gttest2()
Output
20Java Methods
class Parent class Child extends Parent
static void foo() . . . static void
foo() public void testit()
public void testit() P("in Parent's
testit\n") P("in Child's
testit\n") void vtestit()
void vtestit() P("in
Parent's vtestit\n") P("in Child's
vtestit\n") int
main() Parent p new Parent() Child c new
Child() p.testit() p.vtestit() c.testit()
c.vtestit() p c //polymorphic var
(p) p.testit() p.vtestit() p.foo()
c.foo()
21Abstract Methods
- A method that does not include a definition
- A pure virtual function in C
- virtual void absmeth(int i) 0
- Must be declared abstract in Java
- abstract class foo
- abstract void absmeth(int i)
-
- An abstract class includes at least one abstract
method - An abstract class cannot be instantiated
- All classes that inherit from an abstract class
must implement all abstract methods - Java requires that you put the keyword abstract
on class - C, no abstract keyword (1 pure virtual methods)
22Abstract class C
//(C) class Parent class Child public
Parent public public void
testit() void testit() P("in
Parent's testit\n") P("in Child's
testit\n") virtual void
vtestit() 0 void vtestit()
//in java we declare them class
P("in Child's vtestit\n") //abstract
and leave off the 0
int main() Parent p new Parent()
// compiler error class is abstract! Child c
new Child() . . . - Abstract methods (pure
virtual in C) have no method bodies -
Therefore, you cannot make an object instance
from a class with 1 or more abstract methods
23Abstract class - Java
abstract class Employee double rate
abstract double compute_week() Employee(double
rt) rate rt
class Hourly extends Employee double otime_rt
1.5 double otime 0 double
setOtime(double ot) otimeot double
compute_week() return( 40rate
otimeotime_rtrate) Hourly(double rt)
super(rt)
class Salaried extends Employee double
compute_week() return 40rate
Salaried(double rt)super(rt)
Employee Anyone Hourly Joe Joe new
Hourly(12) Joe.setOtime(10) System.err.println(J
oe.compute_week()) / prints 660.0 / Anyone
Joe System.err.println(Anyone.compute_week())
/ prints 660.0 (Joes week)/ Anyone new
Employee(8) /error abstract class cannot be
instantiated /
24Review
- All instance methods in Java are virtual
- Class methods in Java are marked with static
keyword - Should be called using ltClassNamegt.ltstatic
membergt - Instance methods in C are static
- Unless they have the virtual keyword
- Class methods in C are marked with static
keyword - Should be called using ltClassNamegtltstatic
membergt - Virtual methods calls bound to addresses at
runtime - Compiler inserts code to do the lookup
- Static method calls bound to addresses at
compile time - Compiler finds the addresses by looking at the
code