Object Oriented Programming - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Object Oriented Programming

Description:

Mechanism for adding detail to a definition. Enables code reuse and organization ... A method that does not include a definition. A pure virtual function in C ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 25
Provided by: ValuedGate793
Category:

less

Transcript and Presenter's Notes

Title: Object Oriented Programming


1
Object 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)

2
Object-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.

3
Variables
  • 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

4
Methods
  • 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

5
Example Static vs Instance Variables
  • Whats wrong here

Class MyClass int foo static int bar static
void stat_method() foo 1
inst_method() bar 1 void
inst_method() bar 1
6
Object-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

7
Object-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
8
OOP - 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.

9
Another 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
10
Compile errors
  • GraphicalObject obj // Base class (Parent class)
  • Line line // Derived class (Child class)
  • obj line // ok
  • line obj // not ok -- why?

11
Binding 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

12
We 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

13
OOP 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
14
OOP 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
. . .
15
Method 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

16
An 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?

17
Which 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()
18
Virtual 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?
19
C 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
20
Java 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()
21
Abstract 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)

22
Abstract 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
23
Abstract 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 /
24
Review
  • 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
Write a Comment
User Comments (0)
About PowerShow.com