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

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

5 Relationships in model and code relationships exist between objects in problem domains must express these relationships 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 (we’ll talk about object destruction later)

7 Lifetime of a variable Time during execution of a program that the variable exists. –A local variable comes into existence when a method is called, and disappears when the method is completed. –Instance variables are created when a class is instantiated. Instance variables persist as long as their objects persist.

8 Memory organization Process BProcess AProcess C

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

10 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

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

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

13 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)

14 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

31 UML Unified Modeling Language

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

33 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