Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 II1

2 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

3 CSE 116 Introduction to Computer Science For Majors II3 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }

4 CSE 116 Introduction to Computer Science For Majors II4 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.

5 CSE 116 Introduction to Computer Science For Majors II5 And now the gory details and vocabulary review

6 CSE 116 Introduction to Computer Science For Majors II6 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Class definition is shown in green:

7 CSE 116 Introduction to Computer Science For Majors II7 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Instance variable name is shown in green:

8 CSE 116 Introduction to Computer Science For Majors II8 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Instance variable declaration is shown in green:

9 CSE 116 Introduction to Computer Science For Majors II9 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.

10 CSE 116 Introduction to Computer Science For Majors II10 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Constructor definition is shown in green:

11 CSE 116 Introduction to Computer Science For Majors II11 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Header of constructor definition is shown in green:

12 CSE 116 Introduction to Computer Science For Majors II12 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:

13 CSE 116 Introduction to Computer Science For Majors II13 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:

14 CSE 116 Introduction to Computer Science For Majors II14 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:

15 CSE 116 Introduction to Computer Science For Majors II15 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Body of constructor definition is shown in green:

16 CSE 116 Introduction to Computer Science For Majors II16 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”.

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 II17

18 CSE 116 Introduction to Computer Science For Majors II18 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Instantiation of class Tail is shown in green:

19 CSE 116 Introduction to Computer Science For Majors II19 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:

20 CSE 116 Introduction to Computer Science For Majors II20 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:

21 CSE 116 Introduction to Computer Science For Majors II21 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:

22 CSE 116 Introduction to Computer Science For Majors II22 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:

23 CSE 116 Introduction to Computer Science For Majors II23 Class instantiation process by which objects are created example new JButton()

24 CSE 116 Introduction to Computer Science For Majors II24 Class instantiation new + constructor new JButton() new : operator JButton() : constructor call

25 CSE 116 Introduction to Computer Science For Majors II25 Class instantiation 109500 109501 109502 109503 109504 109505 109506 109507 109508 109509 new + constructor new JButton() new : operator JButton() : constructor call

26 CSE 116 Introduction to Computer Science For Majors II26 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.


Download ppt "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."

Similar presentations


Ads by Google