syntax & terminology review while the following slides are not exactly what we did on the board...
Post on 19-Dec-2015
215 views
TRANSCRIPT
CSE 116 Introduction to Computer Science For Majors II 1
Syntax & terminology review
• While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of what we did, and also some details we did not.
CSE 116 Introduction to Computer Science For Majors II 2
Composition
• A whole-part relationship (e.g. Dog-Tail)• Whole and part objects have same lifetime
– Whole creates instance of part in its constructor
• In Java code, involves 3 changes to whole class:– Declaration of instance variable of part class/type– Instantiation of part class in whole class constructor– Assignment of new part instance to instance variable
CSE 116 Introduction to Computer Science For Majors II 3
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
CSE 116 Introduction to Computer Science For Majors II 4
Important points about composition
• Whole has responsibility for creating its parts (which is why instantiation of parts happens in constructor of whole).
• Whole can communicate with parts. This is why an instance variable is declared: to establish a name for the newly created object.
CSE 116 Introduction to Computer Science For Majors II 5
And now
the gory details
and
vocabulary review
CSE 116 Introduction to Computer Science For Majors II 6
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Class definition is shown in green:
CSE 116 Introduction to Computer Science For Majors II 7
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Instance variable name is shown in green:
CSE 116 Introduction to Computer Science For Majors II 8
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Instance variable declaration is shown in green:
CSE 116 Introduction to Computer Science For Majors II 9
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Access control modifiers are shown in green:
Note that access control modifier of _tail is private, not public.
CSE 116 Introduction to Computer Science For Majors II 10
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Constructor definition is shown in green:
CSE 116 Introduction to Computer Science For Majors II 11
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Header of constructor definition is shown in green:
CSE 116 Introduction to Computer Science For Majors II 12
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Access control modifier in header of constructor definition is shown in green:
CSE 116 Introduction to Computer Science For Majors II 13
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Name of constructor in header of constructor definition is shown in green:
CSE 116 Introduction to Computer Science For Majors II 14
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Parameter list in header of constructor definition is shown in green:
CSE 116 Introduction to Computer Science For Majors II 15
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Body of constructor definition is shown in green:
CSE 116 Introduction to Computer Science For Majors II 16
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }
public void bark() {…}}
Suppose we define a public method “bark”.
CSE 116 Introduction to Computer Science For Majors II 17
member access operator
• Fields (instance variables) and methods are collectively known as “members”.
• In: Dog x = new Dog();x.bark();
“.” is the member access operator.
CSE 116 Introduction to Computer Science For Majors II 18
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Instantiation of class Tail is shown in green:
CSE 116 Introduction to Computer Science For Majors II 19
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
‘new’ operator in instantiation of class Tail is shown in green:
CSE 116 Introduction to Computer Science For Majors II 20
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Use of constructor in instantiation of Tail class is shown in green:
CSE 116 Introduction to Computer Science For Majors II 21
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Argument list in instantiation of class Tail is shown in green:
CSE 116 Introduction to Computer Science For Majors II 22
Dog – Tail example in Java
public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }}
Assignment of new Tail instance to instance variable is shown in green:
CSE 116 Introduction to Computer Science For Majors II 23
Class instantiation
• process by which objects are created
• example
new JButton()
CSE 116 Introduction to Computer Science For Majors II 24
Class instantiation
• new + constructor
new JButton()
• new: operator• JButton(): constructor call
CSE 116 Introduction to Computer Science For Majors II 25
Class instantiation
109500
109501
109502
109503
109504
109505
109506
109507
109508
109509
• new + constructor
new JButton()
• new: operator• JButton(): constructor call
CSE 116 Introduction to Computer Science For Majors II 26
Class instantiation
109500
109501
109502
109503
109504
109505
109506
109507
109508
109509
• new JButton() is an expression whose value (in this particular example) is 109500, the starting address of the block of memory storing the representation of the JButton object just created.