Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1.

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1."— Presentation transcript:

1 CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu 1

2

3 Agenda Relationships –in model and code –first relationship: composition Lifetime/Scope –process memory Unified Modeling Language (UML) –composition

4 RELATIONSHIPS

5 Relationships in model and code relationships exist between objects in problem domains want to capture those relationships in our models and express them in our code

6 Composition A whole-part relationship (e.g. Dog-Tail) Whole and part objects have same lifetime –when whole is created, it has its parts –when whole is destroyed, parts go away too

7 whole/part creation in code Whole creates instance of part in its constructor In Java code, involves 3 changes to whole class: –Declaration of variable of part type –Instantiation of part class in whole class constructor –Assignment of new part instance to variable

8 Lifetime issue lifetime of local variable –from invocation of method/constructor –to completion of method/constructor

9 whole/part creation in code Whole creates instance of part in its constructor In Java code, involves 3 changes to whole class: –Declaration of instance variable of part type –Instantiation of part class in whole class constructor –Assignment of new part instance to instance variable

10 LIFETIME / SCOPE

11 Lifetime In addition to scope, variables have another important property called lifetime. The lifetime of a variable is the time during execution of a program that the variable exists.

12 Lifetime of a local variable A local variable comes into existence when a method is called, and disappears when the method is completed.

13 Lifetime of an instance variable Instance variables are created when a class is instantiated. Each object has its own set of instance variables. Instance variables persist as long as their objects persist –as far as we know right now, objects persist until the end of the runtime of the program.

14 Memory organization Process BProcess AProcess C

15 Memory organization Process BProcess AProcess C STATIC SEGMENT RUNTIME STACK FREE/AVAILABLE MEMORY HEAP dynamically allocated memory

16 Local variables Declared inside a method Scope is: –from point of declaration –to end of method body Lifetime is: –from method invocation –to method return

17 Problem! It is often necessary to refer to a variable from many methods in a class. A local variable cannot be used outside of the method in which it is declared.

18 Instance variables A variable declared as a class member (i.e. within the class body but not within any method) is called an instance variable. The scope of an instance variable is the entire class body. Each instance of a class has its own set of instance variables.

19 Expressing it in Java

20 20 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.

21 Class members: (instance) methods & instance variables Any class member (method or variable declared in the class body, but not inside a method) must have an access control modifier. Our rule: methods are public, instance variables are private. Later in semester we will justify this rule (one we know a little more about the issues involved)

22 Instance variable declaration An instance variable declaration consists of an access control modifier in addition to a type and a name. A rule in CSE115 is that all instance variables must be declared using the “private” access control modifier

23 Access Control Modifiers “public” – the member can be accessed from outside the class “private” – the member can be access only from inside the class

24 24 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); }

25 25 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Class definition is shown in green:

26 26 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Instance variable name is shown in green:

27 27 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Instance variable declaration is shown in green:

28 28 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.

29 29 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Constructor definition is shown in green:

30 30 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Header of constructor definition is shown in green:

31 31 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:

32 32 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:

33 33 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:

34 34 Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } Instantiation of class Tail is shown in green:

35 35 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:

36 36 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:

37 37 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:

38 38 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:

39 UML Unified Modeling Language

40 UML Unified Modeling Language –express design without reference to an implementation language For example

41 Composition in UML


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1."

Similar presentations


Ads by Google