ITDP 106 - Information Technology Essentials Elke Vogel-Adham 1
Instantiating an Object
CIS 2403
Object Oriented Programming
ITDP 106 - Information Technology Essentials Elke Vogel-Adham 2
Classes vs. Objects
Instances
 Class = blueprint
 Object
= there can be many
objects of the same
class
= needs to be created
(instantiated)
Movie
name: string
length: int
director: string
Movie()
one
Name: Argo
length: 90
director: Lamont
Movie()
two
name: Titanic
length: 100
director: James
Movie()
three
name: Pride and Prej.
length: 95
director: Ang Lee
Movie()
Instance Variables
Instance Methods
GetName()
GetName()
GetName()
GetName()
ITDP 106 - Information Technology Essentials Elke Vogel-Adham 3
Classes vs. Objects
Instances
Movie
Movie()
one
Name: Argo
length: 90
director: Lamont
Movie()
two
name: Titanic
length: 100
director: James
Movie()
three
name: Pride and Prej.
length: 95
director: Ang Lee
Movie()
Instance Variables
Instance Methods
 Instance Variable
= each Instance has variables with
different values
name: string
length: int
director: string
GetName()
GetName()
GetName()
GetName()
ITDP 106 - Information Technology Essentials Elke Vogel-Adham 4
Instantiating an Object
one
Name: Argo
length: 90
director: Lamont Johnson
Movie ()
new Operator
= instantiates (creates) an object of a class
= allocates space in the computers memory
GetName()
ITDP 106 - Information Technology Essentials Elke Vogel-Adham 5
Instantiating an Object
new Operator – Android and Java
= instantiates (creates) an object of a class
= allocates space in the computers memory
ITDP 106 - Information Technology Essentials Elke Vogel-Adham 6
Instantiating an Object
one
Name: Argo
length: 90
director: Lamont Johnson
Movie ()
Class from which the object is instantiated
Identifier of Object (lower case!)
new Operator
Arguments for Constructor Method
GetName()
ITDP 106 - Information Technology Essentials Elke Vogel-Adham 7
The new Operator
 What is really happening here?
Two spaces in memory are allocated:
 a variable that points (pointer, reference variable) to
an object is created in memory. It stores the address
or reference to the location where the object is stored.
 an object is created in memory.
one
Argo, 90, Lamont Johnson
ITDP 106 - Information Technology Essentials Elke Vogel-Adham 8
The new Operator
 Statement can be split in two!
one Argo, 90, Lamont Johnson
ITDP 106 - Information Technology Essentials Elke Vogel-Adham 9
The new Operator
 Instantiating three objects!
one
Argo, 90, Lamont Johnson
two
three
Titanic, 100, James Cameron
Pride and Prejudice, 95, Ang Lee

3 instantiating an object in c# (1)

  • 1.
    ITDP 106 -Information Technology Essentials Elke Vogel-Adham 1 Instantiating an Object CIS 2403 Object Oriented Programming
  • 2.
    ITDP 106 -Information Technology Essentials Elke Vogel-Adham 2 Classes vs. Objects Instances  Class = blueprint  Object = there can be many objects of the same class = needs to be created (instantiated) Movie name: string length: int director: string Movie() one Name: Argo length: 90 director: Lamont Movie() two name: Titanic length: 100 director: James Movie() three name: Pride and Prej. length: 95 director: Ang Lee Movie() Instance Variables Instance Methods GetName() GetName() GetName() GetName()
  • 3.
    ITDP 106 -Information Technology Essentials Elke Vogel-Adham 3 Classes vs. Objects Instances Movie Movie() one Name: Argo length: 90 director: Lamont Movie() two name: Titanic length: 100 director: James Movie() three name: Pride and Prej. length: 95 director: Ang Lee Movie() Instance Variables Instance Methods  Instance Variable = each Instance has variables with different values name: string length: int director: string GetName() GetName() GetName() GetName()
  • 4.
    ITDP 106 -Information Technology Essentials Elke Vogel-Adham 4 Instantiating an Object one Name: Argo length: 90 director: Lamont Johnson Movie () new Operator = instantiates (creates) an object of a class = allocates space in the computers memory GetName()
  • 5.
    ITDP 106 -Information Technology Essentials Elke Vogel-Adham 5 Instantiating an Object new Operator – Android and Java = instantiates (creates) an object of a class = allocates space in the computers memory
  • 6.
    ITDP 106 -Information Technology Essentials Elke Vogel-Adham 6 Instantiating an Object one Name: Argo length: 90 director: Lamont Johnson Movie () Class from which the object is instantiated Identifier of Object (lower case!) new Operator Arguments for Constructor Method GetName()
  • 7.
    ITDP 106 -Information Technology Essentials Elke Vogel-Adham 7 The new Operator  What is really happening here? Two spaces in memory are allocated:  a variable that points (pointer, reference variable) to an object is created in memory. It stores the address or reference to the location where the object is stored.  an object is created in memory. one Argo, 90, Lamont Johnson
  • 8.
    ITDP 106 -Information Technology Essentials Elke Vogel-Adham 8 The new Operator  Statement can be split in two! one Argo, 90, Lamont Johnson
  • 9.
    ITDP 106 -Information Technology Essentials Elke Vogel-Adham 9 The new Operator  Instantiating three objects! one Argo, 90, Lamont Johnson two three Titanic, 100, James Cameron Pride and Prejudice, 95, Ang Lee