180 likes | 321 Views
Classes. Lecture OO03 Representing Classes. References. Ambler, S., The Object Primer , Cambridge Univ. Press, 2001, Chapter 1 & 2, Sections 5.1 - 5.5 Fowler & Scott, UML Distilled Applying the Standard Object Modelling Language, AWL, 1997, Chapt 6. Teaching Points.
E N D
Classes Lecture OO03 Representing Classes
References • Ambler, S., The Object Primer, Cambridge Univ. Press, 2001, Chapter 1 & 2, Sections 5.1 - 5.5 • Fowler & Scott, UML Distilled Applying the Standard Object Modelling Language, AWL, 1997, Chapt 6
Teaching Points • Introduction to class representations • Introduction to Java classes • Introduction to packages
Review • What is an object? • Why is the object model useful for describing problem domains? • What is a pattern? • Describe the Composite pattern.
OOA/OOD/OOPL Supports • Abstraction • Encapsulation • Modularity • Hierarchy • Typing • Concurrency • Persistance
Abstraction • A UML Class • abstract data type
Recall struct in C Classes in Java struct EmpRec{ char name[45]; int age; char address[256]; }; EmpRec student; student.age = 27;
A Java Class class Vehicle{ private int wheels; private float weight; public void initialize(int wh, float wt) { wheels = wh; weight = wt; } public int getWheels(void) { return wheels; } public float getWeight(void) { return weight; } public float wheelLoading(void) { return weight/wheels; } }
Manipulating an object Vehicle v; v = new vehicle(); v.initialize(4, 1050.0); int w = v.getWheels(); float l = v.wheelLoading();
UML Packages • A mechanism for grouping things • In this course packages will mainly be groupings of classes • Packages can be nested
Java Packages • Each class is a member of a package • An object can ‘use’ the class specifications for other classes in the package of ‘its’ class • To use the specification of classes which are not in its package an object must import the class
Using Java Packages • Explicit names • The package keyword • The import keyword • importing a class • importing a package • The unnamed package • java.lang is always imported
Java Packages and Directories • Strong relationship between packages and directories in the Java environment • The concept of CLASSPATH • resolving a package name
Encapsulation • There must be a mechanism to limit access to the secret of the class. • Members of the class need to access the secret • a class is a scope • Different from block statements • context based on information hiding (often data) and not on procedural decomposition
Access Control • public • protected • unnamed • called :friendly (default) • private • Note: each level of access subsumes the level below it
Modularity • Class is a unit of decomposition • so is a module • Encapsulation is about logical decomposition • Modularity is about physical decomposition • Modules allow independent design, implementation, compiling, revision
Java Modules • Closely related to classes • One public class allowed per file • Multiple non-public classes allowed per file • non-public classes are only visible to the package to which they belong • Note: access control here is similar to that for class members non-public access is like friendly
Teaching Points • Introduction to class representations • Introduction to Java classes • Introduction to packages