260 likes | 459 Views
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. Composition. A whole-part relationship (e.g. Dog-Tail)
E N D
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
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
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
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
And now the gory details and vocabulary review CSE 116 Introduction to Computer Science For Majors II
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
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
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
Dog – Tail example in Java Access control modifiers are shown in green: Note that access control modifier of _tail isprivate, notpublic. public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } CSE 116 Introduction to Computer Science For Majors II
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
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
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
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
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
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
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
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
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
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
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
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
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
Class instantiation • process by which objects are created • example new JButton() CSE 116 Introduction to Computer Science For Majors II
Class instantiation • new + constructor new JButton() • new: operator • JButton(): constructor call CSE 116 Introduction to Computer Science For Majors II
Class instantiation • new + constructor • new JButton() • new: operator • JButton(): constructor call 109500 109501 109502 109503 109504 109505 109506 109507 109508 109509 CSE 116 Introduction to Computer Science For Majors II
Class instantiation • 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. 109500 109501 109502 109503 109504 109505 109506 109507 109508 109509 CSE 116 Introduction to Computer Science For Majors II