csc 205 – java programming ii
DESCRIPTION
CSC 205 – Java Programming II. Lecture 1 Jan 9, 2002. Why OO ? The Big Picture. Software is inherently complex Complexity of the problem domain Difficulty of managing the development process Flexibility possible through software - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/1.jpg)
CSC 205 – Java Programming II
Lecture 1Jan 9, 2002
![Page 2: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/2.jpg)
Why OO? The Big Picture
• Software is inherently complex– Complexity of the problem domain– Difficulty of managing the development
process– Flexibility possible through software– Problems of characterizing the behavior of
discrete systems
![Page 3: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/3.jpg)
Examples of Complex Systems
• A universityAcademics
CLA Engr.… …
CSC Math… … People
Student Employee… …
Graduate UnderGrad Admin. Faculty……
Mercer
HR… Athletics
![Page 4: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/4.jpg)
Attributes of Complex Systems
• Hierarchy: a collection of interrelated subsystems. – Dividable until some lest level is reached
• Arbitrary criterion for choosing subsystems• Cohesive and loosely coupled subsystems
– One component is in charge of certain tasks
– Intra-component communications are less frequent
• Common patterns among subsystems• Evolved from existing simpler systems
![Page 5: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/5.jpg)
Bring Order to Chaos
• The role of decomposition The technique of mastering complexity has been known since ancient time: divide et impera (divide and rule) -- Dijkstra
• OO v.s. procedural decomposition– procedural: top-down structure design with each
component denote a major step in some overall process– OO: viewing the world as a set of autonomous agents
that collaborate to perform some higher level behavior– Which is better?
![Page 6: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/6.jpg)
OO vs Procedural -- Procedural
![Page 7: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/7.jpg)
OO vs Procedural -- OO
![Page 8: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/8.jpg)
Bring Order to Chaos – cont’d
• The role of abstraction– An individual can comprehend only about seven (7)
chunks of information at one time!– Ignore inessential details, deal with the generalized,
idealized model of the object
• The role of hierarchy– Object structure: illustrates how different objects
collaborate with each other– Class structure: highlights common structure and
behavior within a system
![Page 9: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/9.jpg)
Elements of the Object Model
• Abstraction: model the essential attributes
• Encapsulation: hide implementation details
• Modularity: (think package in Java)
• Hierarchy: ranking/ordering of objects
• Typing*: enforcement of the class
• Concurrency*: distinguishes active object
• Persistence*: transcends time and/or space
![Page 10: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/10.jpg)
Abstraction
• Idealized structure model of objects – Recognize similarities– Ignore differences (for the time being)– For a particular purpose
• Provide crisply defined conceptual bounders– Distinguish an object from other kinds of
objects
![Page 11: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/11.jpg)
Abstraction
![Page 12: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/12.jpg)
Abstraction (2)
• Characterize the behavior of an object – by considering the services it provides to other
objects (or its responsibilities) – (client-server) contract model– from the outside view
• Works together with encapsulation– Expose what an object can do and how to request
for services (which constitute the behavior of an object)
– Hide implementation details
![Page 13: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/13.jpg)
Encapsulation
![Page 14: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/14.jpg)
Responsibility
• Responsibilities of an object include– State: information to store
• Implemented with variables in Java
– Operations: • Implemented with methods in Java
• Preconditions: conditions assumed by operations
• Postconditions: conditions satisfied by operations
• Invoke operations by message passing
![Page 15: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/15.jpg)
Message Passing
• Three components that comprise a message: 1. The object to which the message is addressed (YourBicycle) 2. The name of the method to perform (changeGears) 3. Any parameters needed by the method (lowerGear)
![Page 16: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/16.jpg)
Modularity
![Page 17: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/17.jpg)
Hierarchy
• Hierarchy is a ranking or ordering of abstractions
• Two most important hierarchies– The “part-of” relationship, or aggregation– The “is-a” relationship , or inheritance
• Inheritance, also known as subclassing – Single inheritance: the only legal way in Java– Multiple inheritance: supported in C++
![Page 18: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/18.jpg)
Subclassing in Java
• A subclass SavingsAccount:public class SavingsAccount extends Account { private double interestRate;
public double getInterestRate() { return interestRate; }
public void setInterestRate(double rate) { interestRate = rate; }}
![Page 19: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/19.jpg)
The Base Class: Account
public class Account { // Instance variables private double balance;
// Constructors public Account(double initialBalance) { balance = initialBalance; }
public Account() { balance = 0.0; }}
![Page 20: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/20.jpg)
Writing a Subclass
• A SavingsAccount object will contain two variables:• balance, and • interestRate
![Page 21: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/21.jpg)
Writing a Subclass• Methods that can be applied to SavingsAccount objects:– getInterestRate– setInterestRate– deposit (inherited)– withdraw (inherited)– getBalance (inherited)– close (inherited)
![Page 22: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/22.jpg)
Hierarchy: “part-of” relationship
![Page 23: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/23.jpg)
Hierarchy: “is-a” relationship
![Page 24: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/24.jpg)
Typing
![Page 25: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/25.jpg)
Typing Supports Polymorphism
//Shape s = ShapeBuilder.getNextShape();s.draw();//
Shape
+draw()
Oval
+draw()
Rectangle
+draw()
![Page 26: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/26.jpg)
Concurrency
![Page 27: CSC 205 – Java Programming II](https://reader035.vdocuments.us/reader035/viewer/2022062314/568146d2550346895db409a2/html5/thumbnails/27.jpg)
Persistence