330 likes | 409 Views
CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu. Phones off Signs out Picnic on Friday . Agenda. Relationships in model and code first relationship: composition Lifetime/Scope process memory Unified Modeling Language (UML)
E N D
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu
Phones off Signs out Picnic on Friday
Agenda • Relationships • in model and code • first relationship: composition • Lifetime/Scope • process memory • Unified Modeling Language (UML) • Composition
Relationships in model and code • relationships exist between objects in problem domains • must express these relationships in our code
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)
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.
Memory organization Process A Process B Process C
Memory organization Process A Process B Process C STATIC SEGMENT RUNTIME STACK FREE/AVAILABLE MEMORY HEAP dynamically allocated memory
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
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.
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.
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)
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
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
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } }
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Class definition is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Instance variable name is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Instance variable declaration is shown in green:
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(); } }
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Constructor definition is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Header of constructor definition is shown in green:
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:
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:
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:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Instantiation of class Tail is shown in green:
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:
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:
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:
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:
UML Unified Modeling Language
UML • Unified Modeling Language • express design without reference to an implementation language • For example