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(); }


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

Similar presentations


Ads by Google