![Page 1: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/1.jpg)
Object Oriented Methodology
เสถยร หนตาคณะเทคโนโลยสารสนเทศและการสอสารมหาวทยาลยพะเยา
![Page 2: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/2.jpg)
Software development and design
◼ Software
❑ ชดค าสงหรอโปรแกรมทใชควบคมการท างานของเครองคอมพวเตอรทเขยนขนดวยภาษาคอมพวเตอร
❑ สรางขนเพอแกปญหาของผใชและท างานตามทผใชตองการไดอยางถกตอง
![Page 3: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/3.jpg)
Software Development
Software Development Life Cycle
![Page 4: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/4.jpg)
Software Analysis and Design
◼ Structured Analysis and Design
◼ Object Oriented Analysis and Design
![Page 5: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/5.jpg)
Structured Analysis and Design
◼ Data Oriented
❑ Data Flow Diagrams (DFD)
❑ Entity Relationship Diagrams (ERD)
◼ Action Oriented
❑ Finite State Machine (FSM)
![Page 6: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/6.jpg)
DFD Level 0
DFD Level 1
![Page 7: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/7.jpg)
DFD Level 3
![Page 8: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/8.jpg)
DFD
![Page 9: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/9.jpg)
ER Diagram
![Page 10: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/10.jpg)
Object-Oriented Analysis
◼ Builds a real-world model from requirements
❑ client interviews
❑ domain knowledge
❑ real-world experience
◼ The model addresses three aspects of objects
❑ class structure and relationships
❑ sequencing of interactions and events
❑ data transformations and computations
![Page 11: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/11.jpg)
Models of Object-Oriented Analysis
◼ Class Model
❑ static structure
❑ what objects are in the system?
❑ how are they related?
◼ Dynamic Model
❑ behavioral aspects
❑ what events occur in the system
❑ when do they occur and in what
order?
◼ Functional Model
❑ data transformations
❑ “what” does the system do
◼ Data-Oriented
◼ Action-Oriented
◼ Both Data and Actions
![Page 12: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/12.jpg)
Object Oriented Analysis and Design
◼ The Unified Modeling Language (UML)
ตวอยาง
Use-case diagram Class diagram
![Page 13: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/13.jpg)
UML DiagramsStructural
: element of spec. irrespective of time
◼ Class
◼ Component
◼ Deployment
◼ Object
◼ Composite structure
◼ Package
Behavioral: behavioral features of a system / business process
◼ Activity
◼ State machine
◼ Use case
◼ Interaction
Interaction: emphasize object interaction
• Communication (collaboration)
• Sequence
• Interaction overview
• Timing
![Page 14: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/14.jpg)
Structured analysis and design
![Page 15: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/15.jpg)
Object oriented analysis and design
![Page 16: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/16.jpg)
![Page 17: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/17.jpg)
Functions
◼ What we call them:
functions, methods, modules
◼ Functions are a core construct in OO and
non-OO programs
◼ Principals of good functions apply in either
language
❑ But less of a problem in OO – can you think why?
![Page 18: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/18.jpg)
Functions
◼ Functional decomposition❑ แยกปญหาใหเปนสวนยอย
❑ ใช function แกปญหาทละสวน❑ มลกษณะเปนล าดบชน
◼ Problems
❑ การควบคมและการประสานงาน รวมอยท Main method
❑ การเปลยนแปลงท าใหเกดผลกระทบมาก
![Page 19: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/19.jpg)
Functions
◼ Functions, Methods, Modules
◼ Cohesion
◼ Coupling
![Page 20: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/20.jpg)
Cohesion and Coupling
◼ Cohesion (การเกาะยด)❑ การเกาะยด คอระดบของความสมพนธภายใน module
❑ หาก module มการเกาะยดสง แสดงวาสามารถท างานดวยตวของมนเองระดบหนง และมการเรยกใชหรอผกยดกบโปรแกรมตาง module นอย
◼ Coupling (การปฏสมพนธ)❑ การตดตอสงขอมลระหวาง module
❑ ถาระบบใดมปฏสมพนธระหวาง module มาก การยดเกาะภายในจะมโอกาสลดนอยลง
![Page 21: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/21.jpg)
Goal
◼ Flexibility, independence of modules
◼ Looser connections, less brittle system
◼ Reusability
Loosely-coupled modules, each highly
cohesive
![Page 22: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/22.jpg)
Object Oriented Programming
❑ Object
❑ Attributes, Methods and Events.
❑ Inheritance
❑ Encapsulation
❑ Polymorphism
![Page 23: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/23.jpg)
Programming Languages
◼ ภาษาคอมพวเตอรใชในการสรางซอฟแวร◼ แบงเปน 3 กลมหลก ๆ
❑ Machine languages
❑ Assembly languages
❑ High-Level languages
![Page 24: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/24.jpg)
Machine Languages
◼ เปนเลขฐานสอง ประกอบดวย 0 และ 1◼ มความยากในการเขยนโปรแกรมเนองจาก
❑ เปนภาษาทมนษยเขาใจไดยาก❑ ความผดพลาดเพอ 1 จดกท าใหโปรแกรมลมเหลวได❑ ตวอยาง
◼ 1110100010101 111010101110 10111010110100 1010001110111
![Page 25: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/25.jpg)
Assembly Languages
◼ เกดจากการออกแบบตวอกษรภาษาองกฤษใหแทนค าสงรหสเลขฐานสองเรยกวา รหส mnemonic
◼ คอมพวเตอรตองแปลเปนภาษาเครองกอนประมวลผล◼ ตวอยาง
❑ ADD 1001010, 1011010
![Page 26: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/26.jpg)
High-Level Languages
◼ เปนภาษาทท าใหการเขยนโปรแกรมงายขน◼ ใกลเคยงกบภาษามนษย◼ แบงเปน สองกลมหลก
❑ Procedural (Structural) languages
❑ Object-Oriented Languages
![Page 27: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/27.jpg)
Procedural Languages
◼ เปนภาษาระดบสง ทมลกษณะเปนค าสงเรยงล าดบเปนขนตอน และเปนโครงสราง
◼ ตวอยาง❑ C
❑ COBOL
❑ Fortran
❑ LIPS
❑ Perl
![Page 28: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/28.jpg)
Object-Oriented Languages
◼ ใหความสนใจท modeling data ไมใชโครงสราง◼ เขยนโปรแกรมโดยใชแบบแปลนของแบบจ าลอง Classes
◼ ตวอยาง❑ C++
❑ Java
❑ VB.NET
❑ C#
❑ Python
![Page 29: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/29.jpg)
OO Terms and Conceptso Object and Classo Inheritance, Encapsulation and
Polymorphismo Relationship and Interaction between
classes
OO Analysis and Designo Object Modeling with UMLo UML Diagramso Tools
Mapping UML model into OO Programming Languageso Java Language
![Page 30: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/30.jpg)
วตถ
• วตถ (Object)เปน กลมหรอชด (collection) ของสงตอไปน• แอททรบวต (Attribute) ของวตถจะบอกถงขอมล (Data) คณลกษณะเฉพาะหรอ สถานะของวตถ • เมทธอด (Method) จะบอกถงหนาท (functionality) หรอ พฤตกรรม (behavior) ตางๆของวตถนนๆ
• โดยม แอสโซซเอชน (Association) อนไดแก สงทบอกถงความสมพนธ (Relationship) ของวตถนน กบวตถอนๆ รวมทงปฏสมพนธ (Interactions) ระหวางวตถกบวตถอนๆ
![Page 31: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/31.jpg)
การสรางแบบจ าลองของวตถ
• ระบ ขอบเขตของปญหา (Problem Domain) เพอสรางแบบจ าลองวตถ(Modeling Objects)
o เปนการก าหนดขอบเขตของสงทเราจะพจารณาใหเปนวตถ (Object) o เราไมสามารถสนใจทกๆ วตถ (Object) ในโลกของความเปนจรง ในเวลาเดยวกนไดo เชนเดยวกบทเราไมสามารถสนใจทกความสมพนธ และ กจกรรม ทกความสมพนธ และทกกจกรรมทเกดขนในโลกของความเปนจรงไดเชนกน
![Page 32: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/32.jpg)
Objects = Attributes, Methods,
AssociationsAttributes
AgeGenderName….
AssociationsmyLeftArmmyLeftLegmyRightArm....
Methods/OperationsJump..Talk...Dance
Ask FriendJumpWalkJumpShake myHeadShake myBody
Tim
![Page 33: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/33.jpg)
Objects = Attributes, Methods,
AssociationsAttributes
ModelCompanyColor….
AssociationsBrakeLeftFrontWheelGear....
Methods/OperationsStart..Go...Stop..
Go ForwardGo BakcwardTurn LeftTurn RightApply BrakeChange Gear….
รถยนต Toyota สแดง
![Page 34: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/34.jpg)
• ก าหนดคณลกษณะ (Attribute) ใหกบวตถo วตถ Tim มคณลกษณะส าคญคอ อาย (25 ป) เพศ (ชาย) ชอ (Tim) o วตถ รถยนตคนหนง เปน รถ สแดง ยหอ Toyota
• ก าหนดพฤตกรรม/วธการกระท า (Method) ใหกบวตถo วตถ Tim สามารถ เดน ยน นง และนอน ไดo วตถ รถยนตคนหนง สามารถ วง เลยวซาย และเลยวขวาได
• ก าหนดความสมพนธ/ปฏสมพนธกบวตถอน (Association)o วตถ Tim สามารถสรางกจกรรม ใหวตถ รถ เกดการเปลยนแปลงสถานะได เชน การขบรถ
Objects Abstraction
![Page 35: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/35.jpg)
• วตถทม Attribute, Method และ Association ของวตถคลายๆ กนจะถกจดอยในกลมหรอ คลาส (Class) เดยวกน
Group of Objects
![Page 36: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/36.jpg)
• คา Attribute, Method และ Association ของวตถ จะถกก าหนดไวใน “คลาส” (Class)• คลาส (Class)
o เปนสงทน าเสนอ Abstract Data Types (ADTs) ของวตถ (Object) ทเราตองการจ าลองo ก าหนดรายละเอยดเกยวกบ Attribute, Method และAssociation ของวตถ (Object)o เปนพมพเขยว (Blueprint) ของวตถ (Object)
Defining Class
![Page 37: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/37.jpg)
• คลาส (Class) เปนพมพเขยว (Blueprint) ของวตถ (Object)o ไมสามารถน ามาใชไดโดยตรง o ก าหนดคณลกษณะรวมกนในรปของ Attributeo ก าหนดวธการกระท า/พฤตกรรม รวมกนของวตถ ในรปของ Method o ก าหนดความสมพนธ /ปฏสมพนธกบวตถอนในรปของ Association
• เรยกไดวา วตถ (Object) เปนสงทสรางขน หรอเปน อนแสตนซ (Instance) ของคลาส (Class)
Class & Instance
![Page 38: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/38.jpg)
Class & Object
Class
PERSON
Age
Gender
Name
myArms
myLegs
Jump()
Walk()
Talk()
Dance()
ShakeMyArms()
Object “Tim”
Age = 25
Gender = Male
Name = Tim
myArms = Arms
myLegs = Legs
can Jump
can Walk
can Talk
can Dance
can ShakeMyArms
![Page 39: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/39.jpg)
Class & Object
Class
PERSON
Age
Gender
Name
myArms
myLegs
Jump()
Walk()
Talk()
Dance()
ShakeMyArms()Object “Don”
Age = 25
Gender = Male
Name = Don
myArms = Arms
myLegs = Legs
can Jump
can Walk
can Talk
can Dance
can ShakeMyArms
![Page 40: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/40.jpg)
Class & Object
Class คอ ตนแบบขอมลทมไวเพอสราง object ทมโครงสรางพนฐานพฤตกรรมเดยวกน ในแนวคดเชงวตถกอนทสราง object จะตองสราง class ขนมากอน
Object ทกตวจะตองอยใน class ซง class กบobject เปนสงทคกนเสมอ สามารถทราบรายละเอยดและคณสมบต ของ Object ไดดวยการดท Class
![Page 41: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/41.jpg)
Class
Class จะประกอบดวยชอ Class, Attribute และ Method ตาง ๆ ซงเปนตวอธบายรายละเอยด และหนาทตาง ๆ โดยแสดงในลกษณะ diagram ดงน
Name
Attributes
Method
![Page 42: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/42.jpg)
Class
ตวอยาง Class ของ StudentStudent
{abstract}
StudentIDNumber
FirstName
LastName
Address
City
State
RegisterForCourse
DropCourse
ChangeAddress
![Page 43: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/43.jpg)
Class
ใน C++
การสราง Class ท าไดโดยการประกาศ class ซงมสวนประกอบดงน class CRectangle {
int x, y;
public:
void set_values (int,int);
int area (void);
} rect;
attribute
Method
ชอ object สรางจากคลาส
ชอ Class
![Page 44: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/44.jpg)
Class // classes example
#include <iostream>
using namespace std;
class CRectangle {
int x, y;
public: void set_values (int,int); int area () {return (x*y);} };
void CRectangle::set_values (int a, int b) { x = a;
y = b;
}
int main () { CRectangle rect;
rect.set_values (3,4); cout << "area: " << rect.area(); return 0;
}
Method
รายละเอยด Method น ามาเขยนดานลาง Class
การสราง object จาก class
การเรยกใช method
![Page 45: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/45.jpg)
Message passing
◼ การสง Message ไปยงวตถใดๆ แลววตถนนกระท าการอยางใดอยางหนง o เปนการเรยก Method ทถกก าหนดไวในวตถนนมาท างานo แตละวตถ ตองรบผดชอบ พฤตกรรมของตนเอง โดยผานการ
เรยกใช Method
![Page 46: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/46.jpg)
Tim
ถาวตถ Tim ตองการขบรถ aCar
aCaraCar.GoForward(100)aCar.goBackward()
aCar.TurnLeft()
aCar.TurnRight()
aCar.ApplyBrake()
Example of Message Passing
![Page 47: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/47.jpg)
Object-Oriented Software
m1
m2
m4m3
m5
Software
Environment
![Page 48: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/48.jpg)
Characteristics of an Object
◼ Identity
◼ Information Hiding
◼ Encapsulation
◼ Inheritance
◼ Polymorphism
![Page 49: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/49.jpg)
Identity
วตถ (Object) จะตองมลกษณะเฉพาะประจ าตว (Identity)o วตถใดๆ สามารถมคณลกษณะ (Attribute) เหมอนๆ กนได เชน วตถ
“Tim” และ“Don” เปนเพศชาย และอาย 25 ป เหมอนกนo วตถใดๆ สามารถมพฤตกรรม (Behavior/Method) เหมอนๆ กน
ได เชน วตถ“Tim” และ“Don” สามารถกน เดน นอน นง ไดเหมอนกน
o วตถใดๆ สามารถความสมพนธเกยวของ (Association) กบวตถอนๆเหมอนๆ กนได เชน วตถ“Tim” และ“Don” ประกอบดวย 2 แขนและ 2 ขา เหมอนๆ กน
o วตถ “Tim” และ “Don” สรางจากคลาส (Class) เดยวกนคอ “Person”
แตเราถอวา วตถ“Tim” และวตถ “Don” เปนคนละวตถกน
![Page 50: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/50.jpg)
Encapsulation
◼ พจารณาวตถ (Object) ไดเสมอน กลองด า (Black Box)
o วตถใด ตอง “ร” วธหรอขนตอนการท างานภายใน (Method) ของตนเอง
o วตถอน ไมจ าเปนตองรวาวธ หรอขนตอนการท างานภายในเปนอยางไรo วตถอนตองการเรยกใช Method ของวตถนได โดยสง Message มา
โดยท Message จะตองอยในรปแบบทตกลงกนไวกอน
black box
Object
IncomingMessage Outgoing Message
![Page 51: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/51.jpg)
Information Hiding
◼ พจารณาวตถ (Object)
o สามารถก าหนดขอบเขตการเขาถง Attribute และ Method
o ก าหนดวาใครสามารถเหนหรอใช Attribute และ Method ของวตถนนไดบาง
anObject
talk()walk()jump()
NameAge
ObjectDiagram A.
anObject
talk()walk()
jump()
NameAge
Object Diagram B.
anObject
talk()walk()
Jump()
NameAge
Object Diagram C.
![Page 52: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/52.jpg)
Information Hiding and Encapsulation
• ค าถาม: จะค านวณคา “Age” จาก PERSON ไดอยางไร?
o เกบคา วน-เดอน-ปเกด ไว แลวค านวณคา “Age” เมอม การรองขอo เกบคาอายปจจบน (current age) และupdate ทกๆ ป
• ค าตอบ: แลวแตวตถนนจะใชวธไหนตามความถกตองและเหมาะสม โดยก าหนดท Method “getAge()”
PERSON
Age
Gender
Name
myArms
myLegs
Jump()
Walk()
Talk()
Dance()
ShakeMyArms()
getAge()
![Page 53: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/53.jpg)
Advantages of Information Hiding
and Encapsulation◼ Maintainability
o เปนการพจารณาโครงสรางซอฟแวรในระดบนามธรรม (Abstraction) หรอในระดบสง (High-Level) โดยไมค านงถงรายละเอยด วธการ หรอภาษาทจะน าไปพฒนา (Implementation)
◼ Flexibility
o ท าใหการพฒนาซอฟแวรเชงวตถมความยดหยน (Flexibility) สามารถเปลยนแปลงไดงาย (Ease of Change)
o ระบบ Software ใดๆ สามารถปฏบตงานรวมกนไดอยางสะดวก และมประสทธภาพ เนองจากไมจ าเปนตอง “ร” รายละเอยดการท างานภายในของระบบ
![Page 54: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/54.jpg)
JAVA
![Page 55: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/55.jpg)
การแสดงผลเบองตน
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
◼ println เปน static member
method ของ class System
![Page 56: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/56.jpg)
การแสดงผล
Public class Simple
{ pubic static void main(String[] args)
{ System.out.print(“Hello word”);
}
}
System class
Out object
Print method
Method mainชอ class
![Page 57: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/57.jpg)
Method รบขอมล
น าเขาคลาส Scanner เพอเรยกใช method ส าหรบรบขอมล
import java.util.Scanner;
สราง object ชอ input
Scanner input = new Scanner(System.in);
เรยกใช method จาก object input
int x = input.nextInt();
ทดสอบการแสดงผล
System.out.print(“input is ”+x);
![Page 58: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/58.jpg)
package javaapplication1;
import java.util.Scanner;
public class JavaApplication1 {
public static void main(String[] args) {
int x;
Scanner input = new
Scanner(System.in);
x = input.nextInt();
System.out.println("input is "+x);
}
}
![Page 59: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/59.jpg)
Comments are almost like C++
◼ /* This kind of comment can span multiple lines
◼ ใชไดกบหลายบรรทด */
◼ // This kind is to the end of the line ใชกบบรรทดเดยว
◼ /**
* This kind of comment is a special
* ‘javadoc’ style comment
*/
![Page 60: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/60.jpg)
ชนดของขอมล
![Page 61: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/61.jpg)
Primitive data types
◼ Main data types are int, double, boolean, char
◼ Also have byte, short, long, float
◼ boolean has values true and false
◼ Declarations look like C, for example,
❑ double x, y;
❑ int count = 0;
![Page 62: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/62.jpg)
Expressions
◼ Assignment statements mostly look like those in C; you can use =, +=, *= etc.
◼ Arithmetic uses the familiar + - * / %◼ Java also has ++ and --
◼ Java has Boolean operators && || !◼ Java has comparisons < <= == != >= >◼ Java does not have pointers or pointer
arithmetic
![Page 63: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/63.jpg)
Operator
◼ Arithmetic Operator
◼ Relational Operator
◼ Logical Operator
◼ Bitwise Operator
![Page 64: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/64.jpg)
Operator
Arithmetic operators
Shorthand assignment operator
>, >=, <, <=,==, !=
&&, ||, !
Relational Operator
Logical Operator
Bitwise Operator
~,&,|,^,>>,>>>,<<
![Page 65: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/65.jpg)
Operator
![Page 66: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/66.jpg)
PRECEDENCE LEVEL ASSOCIATIVITY Operator
1 R to L ++, --, +, -, ~, !, (data type)
2 L to R *, /, %
3 L to R +, -
4 L to R <<, >>, >>>
5 L to R <, >, <=, >=, instanceof
6 L to R ==, !=
7 L to R &
8 L to R ^
9 L to R |
10 L to R &&
11 L to R ||
12 R to L ?:
13 R to L =, +=, -=, *=, /=, %=, etc.
![Page 67: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/67.jpg)
Control statements
◼ if (x < y) smaller = x;
◼ if (x < y){ smaller=x;sum += x;}
else { smaller = y; sum += y; }
◼ while (x < y) { y = y - x; }
◼ do { y = y - x; } while (x < y)
◼ for (int i = 0; i < max; i++)
sum += i;
conditions must be Boolean !
![Page 68: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/68.jpg)
Control statements II
◼ Java also introduces the trystatement, about which more later
switch (n + 1) {
case 0: m = n - 1; break;
case 1: m = n + 1;
case 3: m = m * n; break;
default: m = -n; break;
}
![Page 69: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/69.jpg)
Java กบ C
◼ In C, almost everything is in functions
◼ In Java, almost everything is in classes
◼ There is often only one class per file
◼ There must be only one public class per file
◼ The file name must be the same as the
name of that public class, but with a .javaextension
![Page 70: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/70.jpg)
Java program layout
◼ A typical Java file looks like:
import java.awt.*;
import java.util.*;
public class Hello {
// object definitions go here
. . .
}
ชอ File ตองตงชอเปน Hello.java
![Page 71: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/71.jpg)
Class
◼ ภาษาคอมพวเตอรยคแรก ๆ จะมแค arrays
❑ ทก ๆ คาใน arrays ตองเปนชนดเดยวกน
◼ ภาษายคตอมาจะม structures (structs)
❑ สามารถมขอมลตางชนดกน อยเปนกลมภายใน structure
◼ Abstract Data Types (ADTs)❑ รวมวธการหรอ function กบขอมลไวดวยกน
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;} book;
![Page 72: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/72.jpg)
Class
◼ A class consists of
❑ a collection of attributes, or variables (คลายกบstructs ในภาษา C)
❑ ม methods ทสามารถจดการกบ attributes
◼ สามารถ น ามาสรางเปน object (can be instantiated)
![Page 73: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/73.jpg)
กฎการตงชอ Class
ธรรมเนยมการตงชอ Class
❑ อกษรตวแรกของชอควรเปนตวอกษรใหญ (capital
letter)
❑ อกษรตวแรกตวแปรอนใหเปนตวอกษรเลก
❑ ไมใช Underscores ในชอ Class
- ชอตองประกอบดวย ตวอกษรตวเลข $ หรอ _ - ชอหามขนตนดวยตวเลข- สามารถตงชอยาว ๆ ได- ชอตวอกษรตวเลกกบตวใหญถอวาตางกน- ตองไมซ ากบค าสงวนของภาษาจาวา- ตองไมซ ากบชออนของโปรแกรมทใชมากอน
![Page 74: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/74.jpg)
An example of a class
class Person {
String name;
int age;
void showage ( ) {
System.out.println (name + ' is now '
+ age);
}
}
![Page 75: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/75.jpg)
Another example of a class
class Driver extends Person {
long driversLicenseNumber;
Date expirationDate;
}
![Page 76: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/76.jpg)
Creating and using an object
◼ Person john;
john = new Person ( );
john.name = "John Smith";
john.age = 37;
◼ Person mary = new Person ( );
mary.name = "Mary Brown";
mary.age = 33;
mary.birthday ( );
![Page 77: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/77.jpg)
An array is an object
◼ Person mary = new Person
( );
◼ int myArray[ ] = new
int[5];
❑ or:
◼ int myArray[ ] = {1, 4,
9, 16, 25};
◼ String languages [ ] =
{"Prolog", "Java"};
array ใน JAVA เปนการใช object แบบหนง
![Page 78: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/78.jpg)
78
Classes
◼ A class is a collection of attributes (data) and
methods (procedure or function) that operate
on that data.
Circle
centre
radius
circumference()
area()
![Page 79: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/79.jpg)
79
Classes
◼ A class is a collection of attributes (data) and methods (procedure or function) that operate on that data.
◼ The basic syntax for a class definition:
class ClassName [extends
SuperClassName]
{
[fields declaration]
[methods declaration]
}
![Page 80: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/80.jpg)
80
Class Circle
◼ Add attributes
◼ The attributes (data) เรยกอกอยางหนงวา instance variables
public class Circle {
public double x, y; // centre coordinate
public double r; // radius of the circle
}
![Page 81: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/81.jpg)
81
Methods
◼ A class with only attribute(data fields) has no life. Objects created
by such a class cannot respond to any messages.
◼ Methods จะประกาศหลงจาก attributes.
◼ The general form of a method declaration is:
type MethodName (parameter-list)
{
Method-body;
}
![Page 82: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/82.jpg)
Method
◼ Static Method
◼ Instance Method
◼ Overloading Method
![Page 83: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/83.jpg)
83
Class Circle
public class Circle {
public double x, y; // centre of the circle
public double r; // radius of circle
//Methods to return circumference and area
public double circumference() {
return 2*3.14*r;
}
public double area() {
return 3.14 * r * r;
}
}
Method Body
![Page 84: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/84.jpg)
84
Data Abstraction
◼ Declare the Circle class, have created a new
data type – Data Abstraction
◼ สามารถประกาศ object ไดดงน
Circle aCircle;
Circle bCircle;
![Page 85: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/85.jpg)
85
Class of Circle
◼ aCircle, bCircle simply refers to a Circle object, not an object itself.
◼ aCircle และ bCircle ชไปยง วตถชนด Circle
aCircle
Points to nothing (Null Reference)
bCircle
Points to nothing (Null Reference)
null null
![Page 86: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/86.jpg)
86
Creating objects of a class
◼ Objects are created dynamically using the
new keyword.
◼ ใช new ในการสรางวตถโดยจะม aCircle และ bCircle ชไปยงวตถนน
◼ aCircle and bCircle refer to Circle objectsbCircle = new Circle() ;aCircle = new Circle() ;
![Page 87: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/87.jpg)
87
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
![Page 88: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/88.jpg)
88
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
x
aCircle
y
bCircle
Before Assignment
x
aCircle
y
bCircle
Before Assignment
//bCircle ถกยายไปช วตถ ท aCircle ชอย
![Page 89: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/89.jpg)
89
Automatic garbage collection
◼ The object does not have a reference and cannot be used in future.
◼ วตถท bCircle ชอยเดมจะไมถกใชอกในอนาคต เพราะไมมตวชหรออางถงอกตอไป
◼ The object becomes a candidate for automatic garbage collection.
◼ Java automatically collects garbage periodically and releases the memory used to be used in the future.
![Page 90: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/90.jpg)
90
การเขาถง attribute
◼ Similar to C syntax for accessing data
defined in a structure.
◼ ใช . ในการอางถง attribute ภายใน object
Circle aCircle = new Circle();
aCircle.x = 2.0 // initialize centre and radius
aCircle.y = 2.0
aCircle.r = 1.0
เชนObjectName.VariableName
ObjectName.MethodName(parameter-list)
![Page 91: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/91.jpg)
91
Executing Methods in Object/Circle
◼ Using Object Methods:
◼ ใช . เพอเรยกใช method ของ object
Circle aCircle = new Circle();
double area;
aCircle.r = 1.0;
area = aCircle.area();
sent ‘message’ to aCircle
![Page 92: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/92.jpg)
92
Using Circle Class// Circle.java: Contains both Circle class and its user class
//Add Circle class code here
class MyMain
{
public static void main(String args[])
{
Circle aCircle; // creating reference
aCircle = new Circle(); // creating object
aCircle.x = 10; // assigning value to data field
aCircle.y = 20;
aCircle.r = 5;
double area = aCircle.area();
double circumf = aCircle.circumference();
System.out.println("Radius="+aCircle.r+" Area="+area);
System.out.println("Radius="+aCircle.r+" Circumference ="+circumf);
}
}
Radius=5.0 Area=78.5
Radius=5.0 Circumference =31.400000000000002
![Page 93: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/93.jpg)
Class example
![Page 94: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/94.jpg)
![Page 95: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/95.jpg)
Multiple class
![Page 96: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/96.jpg)
![Page 97: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/97.jpg)
Constructors
![Page 98: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/98.jpg)
Arguments & Parameters
![Page 99: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/99.jpg)
Passing object to method
![Page 100: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/100.jpg)
![Page 101: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/101.jpg)
![Page 102: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/102.jpg)
Modifier
![Page 103: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/103.jpg)
ตารางแสดงชนดของ Modifier (หรอ Accessibility)
Modifier การใชงาน
static ใชนยามตวแปรและเมธอด ทตองการใหมคาคงท สามารถน าไปใชไดกบทกๆ สวนของClass โดยคานนจะไมมการเปลยนแปลงไมวากรณใดๆ
public ใชนยามตวแปร, เมธอด และคลาส ใด ๆ เพอใหสามารถน าไปใชกบคลาสหรอโปรแกรมอน ๆ ได
private ใชนยามตวแปร หรอเมธอด เพอใหเรยกใชไดเฉพาะภายในคลาส ทสรางตวแปรหรอเมธอดนน ขนมาเทานน
protected ใชนยามตวแปรหรอ เมธอด ทใชไดเฉพาะคลาส ทสรางขนมาดวยวธการสบทอด (Inheritance) เทานน โดยปกตจะใช Protected กบ คลาส ทเปน คลาสตนฉบบ (Base Class)
void ใชนยามเมธอด โดยเปนการก าหนดใหไมมการสงคาใดๆ กลบมาใหกบ เมธอดน (ดงนนจงไมตองใชค าสง return)
![Page 104: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/104.jpg)
modifier
◼ ถาหากไมมการระบคาไว จะหมายถง❑ ตวแปร
◼ ใชไดเฉพาะในเมธอดทนยามตวแปรนนไว
❑ เมธอด◼ ใชไดเฉพาะภายใน คลาส นนเทานน คลาสอนจะเรยกใชไมได
❑ คลาส◼ ใชไดเฉพาะภายในโปรแกรม (File) นเทานน โปรแกรมอนจะมาเรยกใชเมธอดหรอตวแปรตางๆ ใน
คลาส นไมไดเลย
![Page 105: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/105.jpg)
[Static Class Members]
◼ ความหมายคลาสมสมาชกทเปนตวแปรใด ๆ สามารถใชรวมกนไดระหวาง object ทเปน instance ของคลาสเดยวกน (ปกตแยกจากกน)
◼ วธการประกาศใหตวแปรใด ๆ เปนแบบ static class members ตองมค าสงวน static น าหนาชนดขอมล เชน
private static int count = 0;
![Page 106: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/106.jpg)
class X {
static int a = 0;
public static int methodA() {
a += 1;
return a;
}
}
class TestClassMember {
public static void main(String[]
args) {
int num = 0;
num = num +
X.methodA();
}
}
![Page 107: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/107.jpg)
การสงคาผาน Arguments
การสงคาผานทาง arguments ทอยใน parameter list มอย 2 ลกษณะคอ
◼ Argument แบบส าเนาคาขอมล (Pass by Value Argument) ◼ Argument แบบผานคาอางถง (Pass by Reference Argument)
![Page 108: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/108.jpg)
Pass by Value Argument
◼ คณสมบตทเรยกวา Pass by Value คอการสงขอมลไปให method และ methodกอานคาไดโดยตรง และน าไปใชงานไดตามความตองการ แตหากขอมลใน argument เกดการเปลยนแปลงไมวาจากกรณใด ๆ ภายใน method จะไมมผล กบขอมลเดม
การสงคาไปยง method แบบ pass-by-value นคาทสงเขาไปจะถกน าไปใชภายในตว method เทานน การเปลยนแปลงใด ๆ ทเกดขนจะไมมผลกระทบกบตวแปรทเปนเจาของคานน ภายนอก method เลย
![Page 109: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/109.jpg)
Method_Name (Type Argument_Name1,
Type Argument_Name2, …)
{
… Argument_Name1 = …;
…
}
❑ Type คอชนดขอมลทก าหนดเปน int, double, float, boolean❑ Argument_Name1, Argument_Name2 คอชอ argument หากมมากกวาหนงตวจะ
ถกแยก ดวยเครองหมาย , (comma)
![Page 110: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/110.jpg)
Pass by value
![Page 111: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/111.jpg)
Pass by Reference Argument
◼ คณสมบตทเรยกวา Pass by Reference คอการสงขอมลไปให method น าไปใชงานไดตามความตองการ แตหากขอมลใน argument ไมวาจะเปนการเปลยนแปลง attribute หรอการสรางใหม การเปลยนแปลงดงกลาวภายใน method ขอมลทเปลยนแปลงไปจะมผลกบขอมลหลงจาก method ท างานเสรจสน
◼ การสงคาแบบอางองนน สงทเราสงเขาไปใน method นนไมใชคาของตวแปร (copy) แตเปนตวอางองถง object นน ๆ โดยตรง เราอาจพดไดวาสงทเราสงไปนน เปนทอย (address) ของ object นน ๆ กได
◼ ลกษณะการสงขอมล argument จะไมใชการส าเนาขอมลเพอน าไปใชงานภายใน method แตใน method จะไดรบเปน จดอางถง (address) หรอจดทเชอมโยงไปหา object ดงนนจงเรยกการสงขอมล แบบนวาการผานคาอางถง
![Page 112: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/112.jpg)
Method_Name (Class_Name Argument_Name1, Class_Name Argument_Name2, …)
{ Argument_Name1.Attribute_Name =
…;
Argument_Name2.Method_Name();
… }
❑ Class_Name คอชอ class ทเปนทมาของ object ทจะสงมาเปน argument❑ Argument_Name1, Argument_Name2 เปน argument ทถกสรางขนมาแบบ object
(สรางมาจาก class ดวยค าสง new)
![Page 113: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/113.jpg)
Passing object to method
![Page 114: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/114.jpg)
![Page 115: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/115.jpg)
![Page 116: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/116.jpg)
Package
◼ โปรแกรมทมขนาดใหญ จะประกอบดวย class จ านวนมาก เราสามารถจดหมวดหมใหเปนระเบยบและงายตอการใชงานได ดวยการแยก source code ของโปรแกรมหนงโปรแกรมใหเปนหลาย file ในกรณน source code ไมจ าเปนตองม method main() อย ยกเวนซอรสโคดทมคลาส คลาสแรกทเราใชเรยกโปรแกรมหลกของเรา
◼ เราเรยก file ทประกอบดวยคลาสตงแตหนงคลาสขนไปแตไมมคลาสทมเมออด main() อยวา แพคเกจ
◼ เปนทรวมของ class ตาง ๆ ทถกเรยกใชในโปรแกรม เพอท าใหการเรยกใช class ตาง ๆ เหลานท าไดงายขน
![Page 117: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/117.jpg)
Package
◼ คอการรวมกลมของคลาสไวในชดเดยวกน◼ เพอความสะดวกในการอางองถงเพอใชงาน◼ ท าใหการจดเกบ Bytecodes เปนไปอยางมระบบ◼ ใชหลกเกณฑการจด package เปนไปตามการจด Directory ของระบบ◼ ท าใหเกดรปแบบหนงของการก าหนดระดบการเขาถง
![Page 118: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/118.jpg)
Java Package
![Page 119: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/119.jpg)
Package
◼ ค าสงในการเรยกใช Package คอ ค าสง import
◼ ตวอยางการใชงาน❑ import java.util.*;
หมายถง เรยกใช Package ชอ “java.util” ทง Package (ทก Class)❑ import java.util.ArrayList;
หมายถง เรยกใชเฉพาะ Class ชอ “ArrayList” ซงอยใน Library ชอ “java.util”
![Page 120: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/120.jpg)
Class Declaration Every program must include at least one class.
![Page 121: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/121.jpg)
Encapsulation (Protection)
Encapsulation : คอรากฐานอยางหนงของแนวคดในเชง Object-OrientedซงขอดของEncapsulation คอการปองกน Attribute ของ Object จากความเสยหาย
Encapsulation คอการหอหม Attribute และ Methods เขาไวดวยกน
Encapsulation จะท าหนาทปองกนมให Object อนทอยภายนอก เขาถงObject หนงๆ ไดอยางอสระจะมเฉพาะ Methods ทอยใน Object เทานนจะสามารถตดตอกบ Attribute ทอยใน Object เดยวกนได
![Page 122: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/122.jpg)
Encapsulation
◼ หมายถงการจะเรยกใชคณลกษณะของวตถ จะท าไดโดยการเรยกผานเมธอดเทานน
◼ หลกการของการหอหม คอการก าหนดใหคณลกษณะของออปเจคมคณสมบตเปน private และก าหนดใหเมธอดมคณสมบตเปน public
◼ โดยมเมธอด get / set ไวเพอเขาถง data นนๆจะเรยกวา class นน ๆ เปน Full Encapsulation class
![Page 123: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/123.jpg)
Encapsulation
◼ การก าหนดการเขาถง ใน C++ ก าหนดไว 3 ระดบดงน❑ Public
❑ Protected
❑ Private
![Page 124: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/124.jpg)
JAVA
public private
Attribute ใหภายนอกเขาใชงานได ใชไดส าหรบภายในเทานน
Method ใชตดตอกบภายนอก ใชภายใน class เทานน
![Page 125: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/125.jpg)
Encapsulation
◼ จะใช Access Modifiers ในการเขาถงขอมล และ method
❑ Public class อนภายนอก สามารถทจะเขาถงใด❑ Private class อนภายนอก ไมสามารถเขาถงใด❑ Protected class ทถกสบทอดมาเทานนทสามารถใชได❑ ถาไมก าหนด จะถอวา friendly หรอ default โดยจะเปน public ใน
package เดยวกน แตเปน Private ส าหรบ package อน
![Page 126: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/126.jpg)
Encapsulation
![Page 127: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/127.jpg)
Inheritance
◼ การถายทอดคณสมบตไปยงClass ลก❑ Class ทเปนตนแบบเรยกวา Superclass
❑ Class ทไดรบการสบทอดคณสมบตเรยกวา Subclasses
❑ Class หนงจะม Superclass ได Class เดยว
◼ ซงประโยชนของ Inheritance คอ❑ ท าใหมโครงสรางทเปนระบบ ระเบยบ ปรบเปลยนไดงาย❑ ลดเวลาในการพฒนาระบบ❑ ลดคาใชจายในการพฒนา
![Page 128: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/128.jpg)
Inheritance
◼ Java ใช extends ชอclass
◼ ในการสบทอด◼ class x extends y
◼ {
◼ }
Class x
Class y
![Page 129: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/129.jpg)
◼ Constructors
❑ เปน method พเศษทจะถกเรยกใหท างานตอนท Object ถกสรางขนมา❑ คลาส ๆ หนงอาจม Constructor หลายตวได แตตองม Argument ตางกน
เรยกวา Overloading Constructors โดยพารามเตอรทสงไปยง constructor ตองมจ านวนไมเทากน หรอ ถาจ านวนเทากน ชนดของพารามเตอรตองไมเหมอนกน
❑ สวนใหญจะน ามาใชในการก าหนดคาเรมตนใหกบ Object
❑ รปแบบการใชใน Java คอ public Classname()
Constructors
![Page 130: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/130.jpg)
constructors
![Page 131: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/131.jpg)
final
◼ ถาระบอยหนา class จะท าให class อนๆไมสามารถทจะสบทอดคณสมบตของ class ทระบ final ได
◼ ถาระบอยหนา method จะท าให class ทสบทอดไปไมสามารถทจะ Override
method นได
final class sample
{ …..
}
class test extends sample
{}
![Page 132: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/132.jpg)
Composition
Car
Wheel
Door
Engine
Class car{
Wheel [] theWheel = new Wheel[4];
Door [] theDoor = new Door[2];
Engine theEngine = new Engine();
}
![Page 133: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/133.jpg)
Polymorphism
◼ วตถตาง ๆ ทมพฤตกรรมแบบเดยวกนอยในตว แตรายละเอยดวธการกระท าอาจแตกตางกนได ขนอยกบวาวตถนนถกน าไปใชแบบใดo เชน วตถ “SQUARE” และวตถ “CIRCLE” ตางกสามารถถก
“calculateArea” ได แตวธการ “calculateArea” ของทง 2 วตถอาจไมเหมอนกน
◼ วตถตาง ๆ มวธคณลกษณะ และรายละเอยดวธการกระท าทแตกตางกน แตมองใหเหมอนกนได จากคณลกษณะและพฤตกรรมทวตถเหลานนมรวมกนo เชน วตถ “SQUARE” และวตถ “CIRCLE” มวธการ
“calculate Area” ตางกน แตสามารถมองคณสมบตรวมกนอนไดแก ทง 2 วตถ ตางกเปน วตถ “SHAPE” ทสามารถ “calculate Area” ได
![Page 134: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/134.jpg)
polymorphism
◼ public interface Vegetarian{}
◼ public class Animal{}
◼ public class Deer extends Animal implements Vegetarian{}
A Deer IS-A Animal
A Deer IS-A Vegetarian
A Deer IS-A DeerA Deer IS-A Object
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;Object o = d;
![Page 135: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/135.jpg)
Example
/* File name : Employee.java */
public class Employee {
private String name;
private String address;
private int number;
public Employee(String name, String address, int number) {
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public void mailCheck() {
System.out.println("Mailing a check to " + this.name + " " +
this.address);
}
public String toString() {
return name + " " + address + " " + number;}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public int getNumber() {
return number;
}}
![Page 136: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/136.jpg)
/* File name : Salary.java */
public class Salary extends Employee {
private double salary; // Annual salary
public Salary(String name, String address, int number, double
salary) {
super(name, address, number);
setSalary(salary);
}
public void mailCheck() {
System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName()
+ " with salary " + salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
if(newSalary >= 0.0) {
salary = newSalary;
}
}
public double computePay() {
System.out.println("Computing salary pay for " +
getName());
return salary/52;
}}
![Page 137: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/137.jpg)
/* File name : VirtualDemo.java */
public class VirtualDemo {
public static void main(String [] args) {
Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
System.out.println("Call mailCheck using Salary reference --");
s.mailCheck();
System.out.println("\n Call mailCheck using Employee reference--");
e.mailCheck();
}}
![Page 138: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/138.jpg)
Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
ailing check to Mohd Mohtashim with salary 3600.0
Call mailCheck using Employee reference--
Within mailCheck of Salary classailing check to John Adams with salary 2400.0
Reference code:https://www.tutorialspoint.com/java/java_abstraction.htm
![Page 139: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/139.jpg)
Polymorphism
◼ หมายถง การบอกแบบเดยว แตไดรบการสนองไดหลายแบบ
◼ Overloading
❑ Method มชอซ ากนไดแตม argument แตกตางกน
◼ Overriding
❑ น า Method ของ Superclass มาจดการไดเอง
![Page 140: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/140.jpg)
Overloading Methods
◼ เมธอดใดๆ ทสามารถรบคาทเปนตวแปรไดหลากหลายชนดได ถาก าหนดไวเปนเมธอดเดยว อาจท าใหผลลพธทไดเกดความผดพลาดได
◼ เมธอดเดยวกน แตสรางใหเมธอด มชนดของตวแปรใน parameter list ตางกน ◼ เมอมการใชเมธอด ชอนโปรแกรมจะเรยกเมธอดทมชนดของตวแปรตรงกบทตองการโดยอตโนมต ◼ เรยกวอการนวา “Overloading” และเรยกเมออดทใชวอการนวา “Overloading Methods”
![Page 141: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/141.jpg)
Overloading
public int add(int x, int y)
{ return x+y;
}
public double add(double x, double y)
{ return x+y;
}
public int subtract(int x, int y)
{ return x-y;
}
public double subtract(double x, double y)
{ return x-y;}
![Page 142: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/142.jpg)
Overriding
◼ เปนการจดการ implement
◼ method ทเปนของ Super
Class
![Page 143: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/143.jpg)
abstract class And Methods
◼ ใชส าหรบ ให class อน ๆ สบทอดไปใชงาน◼ ไมสามารถ ทจะสราง object จาก class
abstract ได
![Page 144: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/144.jpg)
abstract class And Methods
testAbstractClass t=new testAbstractClass();
Error
![Page 145: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/145.jpg)
abstract class And Methods
จ าเปนตอง Override
![Page 146: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/146.jpg)
Interfaces
◼ มลกษณะ เหมอน Abstract Class แตจะไมม fields
◼ จะไมมการ implement Code ภายใน methods
ประกาศ
เรยกใช
![Page 147: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/147.jpg)
Interfaces
![Page 148: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/148.jpg)
Graphical User Interface (GUI)
Applications
148
Abstract Windowing Toolkit (AWT)
Events Handling
Swing
![Page 149: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/149.jpg)
AWT - Abstract Windowing Toolkit
◼ Single Windowing Interface on Multiple Platforms
◼ Supports functions common to all window systems
◼ Uses Underlying Native Window system
◼ AWT provides❑ GUI widgets
❑ Event Handling
❑ Containers for widgets
❑ Layout managers
❑ Graphic operations
149
![Page 150: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/150.jpg)
AWT - Abstract Window Toolkit
◼ Portable GUI - preserves native look and feel
◼ Standard GUI Components (buttons…)
◼ Containers - Panels, Frames, Dialogs
◼ Graphics class for custom drawing
◼ Layouts responsible for actual positioning of components:
❑ BorderLayout, GridLayout, FlowLayout, Null layout
150
![Page 151: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/151.jpg)
Building Graphical User Interfaces
◼ import java.awt.*;
◼ Assemble the GUI
❑ use GUI components,
◼ basic components (e.g., Button, TextField)
◼ containers (Frame, Panel)
❑ set the positioning of the components
◼ use Layout Managers
◼ Attach events
151
![Page 152: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/152.jpg)
A sample GUI program
import java.awt.*;
public class MyGui
{
public static void main(String args[] )
{
Frame f = new Frame ("My Frame");
Button b = new Button("OK");
TextField tf = new TextField("Programming in Java", 20);
f.setLayout(new FlowLayout());
f.add(b);
f.add(tf);
f.setSize(300, 300);
f.setVisible(true);
}
}
152
![Page 153: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/153.jpg)
Events
◼ Each GUI component (e.g., a Button) that wishes to respond to an event type (e.g., click), must register an event handler, called a Listener.
◼ The listener is an object of a "Listener" interface.
◼ A Listener class can be created by subclassing (through "implements") one of Listener interfaces (all listener inrefaces are in the java.awt.event package = > must import java.awt.event.*; )
◼ The registration of the listener is done by a call to a method such as addActionListener(<Listener Object>). Each GUI component class has one or more such add…() methods, where applicable.
153
![Page 154: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/154.jpg)
Events
154
b.addActionListener( );
method to add a listener listener objectButton
f.addWindowListener( );
Frame
![Page 155: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/155.jpg)
Listener Interfaces in java.awt.event.*
◼ [1] ActionListener
◼ [2] ItemListener
◼ [3] MouseMotionListener
◼ [4] MouseListener
◼ [5] KeyListener
◼ [6] FocusListener
◼ [7] AdjustmentListener
◼ [8] ComponentListener
◼ [9] WindowListener
◼ [10] ContainerListener
◼ [11] TextListener
155
![Page 156: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/156.jpg)
Listener Interfaces
◼ Each listener interface has methods that need to be implemented for handling different kinds of events.
◼ For example 1, the ActionListener interface has a method actionPerformed() button component is operated.
◼ For example2, the MouseMotionListener interface has two methods: ❑ 1) mouseDragged(MouseEvent) - Invoked when a mouse button
is pressed on a component and then dragged.
❑ 2) mouseMoved(MouseEvent) - Invoked when the mouse button has been moved on a component (with no buttons down).
156
![Page 157: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/157.jpg)
Implementing the ActionListener Interface and
attaching an event handler to a button
import java.awt.*;
import java.awt.event.*;
public class MyGui1
{
public static void main(String args[] )
{
Frame f = new Frame ("My Frame");
MyGuiAction ga = new MyGuiAction(f);
}
}
class MyGuiAction implements ActionListener
{
static int count = 0;
Button b;
TextField tf;
MyGuiAction(Frame f)
{
157
b = new Button("OK");
b.addActionListener(this);
tf = new TextField("Hello Java", 20);
f.setLayout(new FlowLayout());
f.add(b);
f.add(tf);
f.setSize(300, 300);
f.setVisible(true);
}
public void actionPerformed( ActionEvent e)
{
if(e.getSource() == b)
{
count++;
System.out.println("Button is Pressed");
tf.setText("Hello Java Click "+count);
}
}
}
![Page 158: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/158.jpg)
BorderLayout Example
import java.awt.*;
public class MyGui2
{
public static void main(String args[] )
{
Frame f = new Frame ("My Frame");
f.setLayout(new BorderLayout());
// Add text field to top
f.add("North",new TextField());
// Create the panel with buttons at the bottom...
Panel p = new Panel(); // FlowLayout
p.add(new Button("OK"));
p.add(new Button("Cancel"));
f.add("South",p);
f.add("Center", new TextField("Center region"));
f.setSize(300, 300);
f.setVisible(true);
}
}
158
![Page 159: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/159.jpg)
AWT to Swing
◼ AWT: Abstract Windowing Toolkit◼ import java.awt.*
◼ Swing: new with Java2◼ import javax.swing.*
◼ Extends AWT
◼ Tons o’ new improved components
◼ Standard dialog boxes, tooltips, …
◼ Look-and-feel, skins
◼ Event listeners
![Page 160: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/160.jpg)
Object
Component
Container ButtonList
Scrollbar
Label
Canvas
WindowJComponent
Scrollpane
![Page 161: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/161.jpg)
Swing does not replace the AWT; it is built on top of it
All 1.0 AWT components are heavyweight; corresponding Swing components are lightweight
Swing component names begin with ``J'': Component (AWT) vs. JComponent (Swing) Button (AWT) vs. JButton (Swing)
Always use Swing components; however, since Swing is built on top of AWT, you will need to know some AWT methods
![Page 162: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/162.jpg)
JComponent
AbstractButton
JButton JMenuItem
JToggleButton
JCheckBox
JLabel
JList
JScrollBar
JFileChooser
![Page 163: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/163.jpg)
Note that JComponents are containers
JComponents do not extend their AWT counterparts: For example, the JButton class is not a subclass
(direct or indirect) of Button
However, some Swing components are not JComponents
For example, some Swing containers are direct subclasses of their AWT counterparts
![Page 164: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/164.jpg)
Container
JComponent Panel
ScrollPane
Window
Dialog Frame
Applet
![Page 165: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/165.jpg)
Container
JComponent Panel
ScrollPaneWindow
Dialog Frame Applet
JDialog
JWindow
JFrame JApplet
![Page 166: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/166.jpg)
JComponent
JLayeredPane JPanel
JScrollPane
JInternalFrame
JDesktopPane
JTableJTree
![Page 167: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/167.jpg)
Object Oriented Analysis and Design
![Page 168: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/168.jpg)
168
◼ Object Oriented Analysis❑ Determine what to do
❑ Extract Objects (modules)
◼ Object Oriented Design❑ Determine how to build
◼ Unified Modeling Language (UML) ❑ Is a modeling language for documenting OO Analysis
and Design.
![Page 169: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/169.jpg)
169
Solving the Problem
◼ Step 1 - Write Requirements for the system
❑ Should be clearly written and unambiguous
❑ Should be implementable
❑ Should be testable
❑ Is not a part of this course
◼ Step 2 – Analysis, Design (Modeling)
❑ You will be learning in this section…..
◼ Step 3 – Implementation
❑ You already know…..
![Page 170: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/170.jpg)
170
Analysis, Design and Implementation
◼ Proper analysis and design (modeling) prior
to implementation results in a high quality
product.
◼ Successful projects spend most time on
analysis, less on design and even less time
on implementation.
Analysis Design Implementation
![Page 171: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/171.jpg)
Software Development Process and Unified
Modeling Language (UML)
◼ A software development process is a set of phases that are followed to bring a product or a system from conception to delivery.
◼ In the Unified Process, there are four of these phases:❑ Analysis phase
◼ identify the system we are going to develop, including what it contains and its business case.
◼ UML: use-case diagrams
❑ Design phase◼ perform detailed design and identify the foundation of system from “use case
diagram”, which eventually lead to classes.
◼ UML: classes, objects, class diagrams, sequence diagram, collaboration diagrams etc.
❑ Implementation phase (write software using Java/C++)◼ the actual building of the product from the design of the system.
❑ Transition (Rolling out phase): Deliver the system/product to the users. Includes maintenance, upgrades, and so on until phasing out.
![Page 172: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/172.jpg)
172
Introduction to UML
◼ Is a graphical modeling language that can be
used represent the artifacts of analysis and
design.
◼ It is a standard that has international support.
◼ Was developed by Rational Software –
❑ Grady Booch, Jim Rumbaugh and Ivar Jacobson
![Page 173: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/173.jpg)
173
UML – Building Blocks
◼ Elements (Things)
❑ e.g. classes, interfaces etc
◼ Relationships
❑ e.g. association, generalization
◼ Diagrams
❑ e.g. class diagrams, use case diagrams
![Page 174: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/174.jpg)
174
UML - Elements
centreX:IntcentreY:Int=0
Circle
draw() move(Int X, Int Y)
centreX:IntcentreY:Int=0
CircleA:Circle
draw() move(Int X, Int Y)
Class Object
<<interface>>
TypeWriter
keyStroke()
Interface
Borrow
Use-case
Actor
Shapes
Component
![Page 175: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/175.jpg)
UML - Relationships
◼ Dependency
◼ Association
◼ Generalization
◼ Realization
![Page 176: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/176.jpg)
UML - Diagrams
◼ Structural Diagrams❑ Visualize, specify, construct and document the
STATIC aspects of the system.
◼ Behavioral Diagrams❑ Visualize, specify, construct and document the
DYNAMIC aspects of the system.
![Page 177: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/177.jpg)
UML– Diagrams – cont..
Structural
Class Diagram
Object Diagram
Component Diagram
Deployment Diagram
Behavioral
Use case Diagram
Sequence Diagram
Collaboration Diagram
Statechart Diagram
Activity Diagram
![Page 178: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/178.jpg)
UML– Diagrams - Notes
◼ Class Diagram shows a set of classes, interfaces and
their relationships.
◼ Object Diagram shows a set of objects and their
relationships.
◼ Component Diagram shows the organization and
dependencies among a set of of components.
◼ Deployment Diagram shows the configuration of run
time processing nodes.
![Page 179: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/179.jpg)
UML– Diagrams - Notes
◼ Use Case Diagram shows use cases, actors and their
relationships.
◼ Sequence Diagram shows the interaction between
actors and objects and other objects of the system in
ordered based on the time they occur.
◼ Collaboration Diagram shows the structural
organization of the objects that send and receive
messages.
◼ Statechart Diagram shows a state machine consisting
of states, transitions an events.
◼ Activity Diagram used to analyze the bahaviour within
more complex use-cases.
![Page 180: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/180.jpg)
Life Cycle Phase
◼ Requirements
◼ Object Oriented Analysis
◼ Object Oriented Design
◼ Development
◼ Deployment
![Page 181: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/181.jpg)
Analysis
Activity Diagram
Use-case DiagramUnderstand
System Usage
UML DiagramActivity
Identify Classes
Define Workflows
High Level Class Diagram
![Page 182: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/182.jpg)
Design
Sequence and
Collaboration Diagram
Identify Interactions
among objects
State DiagramAnalyze State
Changes
Class DiagramRefine Class
Diagrams
UML DiagramActivity
![Page 183: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/183.jpg)
Use Case Diagrams
![Page 184: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/184.jpg)
Introduction
◼ Getting started is the most difficulty part of any new process.
◼ In software modelling, the first thing you need to do is understand what are you going to model and ultimately develop.
◼ Creating a highest form details about a system--use case diagram--is an almost natural point of origin for the software design.
◼ A use case diagram is an excellent way to communicate to management, customers, and other non-development people what a system will do when it is completed.
![Page 185: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/185.jpg)
University Record System (URS)
◼ A University record system should keep information about its students and academic staff.
◼ Records for all university members are to include their id number, surname, given name, email, address, date of birth, and telephone number. ❑ Students and academic staff each have their own unique ID
number: studN (students), acadN (academic employee), where N is an integer (N>0).
◼ In addition to the attributes mentioned above: ❑ Students will also have a list of subjects they are enrolled in. A
student cannot be enrolled in any more than 10 subjects.
❑ Academic employees will have a salary, and a list of subjects they teach. An academic can teach no more than 3 subjects.
![Page 186: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/186.jpg)
Some Actions Supported by URS
◼ The system should be able to handle the
following commands.
❑ Add and remove university members (students,
and academic staff)
❑ Add and Delete subjects
❑ Assign and Un-assign subjects to students
❑ Assign and Un-assign subjects to academic
staff.
![Page 187: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/187.jpg)
Use Case Diagrams
◼ Use Case diagrams show the various activities the users can perform on the system.
❑ System is something that performs a function.
◼ They model the dynamic aspects of the system.
◼ Provides a user’s perspective of the system.
![Page 188: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/188.jpg)
Use Case Diagram - URS System
systemuser academic
student
URS
del member
add member
add subject
del subject
assg subject
unass subject
enrol subject
unenrol subject
![Page 189: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/189.jpg)
Use Case Diagrams
◼ A set of ACTORS : roles users can play in interacting with the system.❑ An actor is used to represent something that users our
system.
◼ A set of USE CASES: each describes a possible kind of interaction between an actor and the system.❑ Uses cases are actions that a user takes on a system
◼ A number of RELATIONSHIPS between these entities (Actors and Use Cases).❑ Relationships are simply illustrated with a line
connecting actors to use cases.
![Page 190: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/190.jpg)
Use Case Diagrams - Actors
◼ An actor is a user of the system playing a
particular role.
◼ Actor is shown with a stick figure.
employee clientemployer
![Page 191: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/191.jpg)
Use Case Diagrams – Use Cases
◼ Use case is a particular activity a user can do
on the system.
◼ Is represented by an ellipse.
◼ Following are two use cases for a library
system.
ReserveBorrow
![Page 192: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/192.jpg)
Use Case Diagram – Example (Library)
client employee
supervisor
library system
borrow
reserve
Order title
Fine payment
![Page 193: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/193.jpg)
Use Case Diagrams - Relationships
◼ Inclusion❑ Inclusion enables to reuse one use case's steps inside
another use case.
◼ Extension❑ Allows creating a new use case by adding steps to
existing use cases
◼ Generalization❑ Allows child use cases to inherit behavior from parent
use cases
![Page 194: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/194.jpg)
Use Case – Example (self service
machine)
Restock
Supplier
Self service machine
Buy a product
customer
Self service machine
Collect Money
Self service machine
Collector
![Page 195: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/195.jpg)
Use Case – Example (self service machine – includes
relationship)
Close Machine
Restock
Close Machine
Open Machine
<<includes>>
<<includes>>
CollectOpen Machine
<<includes>>
<<includes>>
![Page 196: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/196.jpg)
Use Case – Example (self service machine –
extends relationship)
Restock
Close Machine
Open Machine
<<includes>>
<<includes>>
Restock According
to Sales
<<extends>>
![Page 197: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/197.jpg)
Use Case – Example (self service machine – generalize
relationship): Actor-to-Actor relationship
Supplier Agent
Restocker Collector
generalized actor
specialized actor
![Page 198: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/198.jpg)
Use Case – Example (self service
machine)
Close Machine
Restock
Close Machine
Open Machine
<<includes>>
<<includes>>
Collect
Open Machine
<<includes>>
<<includes>>
Buy a product
Restock according to sales
customer
supplier
Self Service Machine
![Page 199: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/199.jpg)
199
Class Diagrams
![Page 200: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/200.jpg)
200
Identify Classes (Extract Nouns)
❑ A University record system should keep information about its students and academic staff.
❑ Records for all university members are to include their idnumber, surname, given name, email, address, date of birth, and telephone number.
◼ Students and academic staff each have their own unique ID number: studN (students), acadN (academic employee), where N is an integer (N>0).
❑ In addition to the attributes mentioned above:
◼ Students will also have a list of subjects they are enrolled in. A student cannot be enrolled in any more than 10 subjects.
◼ Academic employees will have a salary, and a list of subjects they teach. An academic can teach no more than 3 subjects.
![Page 201: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/201.jpg)
201
Nouns which are potential classes
❑ A University record system should keep information about its students and academic staff.
❑ Records for all university members are to include their idnumber, surname, given name, email, address, date of birth, and telephone number.
◼ Students and academic staff each have their own unique ID number: studN (students), acadN (academic employee), where N is an integer (N>0).
❑ In addition to the attributes mentioned above:
◼ Students will also have a list of subjects they are enrolled in. A student cannot be enrolled in any more than 10 subjects.
◼ Academic employees will have a salary, and a list of subjects they teach. An academic can teach no more than 3 subjects.
![Page 202: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/202.jpg)
202
Classes identified in the first pass
❑ UniversityRecordSystem - URS
❑ Student
❑ Academic Staff
❑ UniversityMembers
❑ Subject
![Page 203: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/203.jpg)
URS - High Level Class Diagram
URSDataBase
UniversityMember
AcademicStaff Student
Subject
1
*has
1
*
has
teaches
0..3
1
takes *
0…10
![Page 204: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/204.jpg)
Modeling a class in UML
◼ When modeling a class in UML, we have a lot of flexibility.
◼ The same class can be modeled in four different ways:❑ With no attributes or operations shown
❑ With only the attributes shown
❑ With only the operations shown
❑ With both the attributes and operations shown
◼ The name of the class is always the first component of the class box; in fact, it is the only required component, as we have seen in our earlier discussion (OOP in Java).
![Page 205: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/205.jpg)
Classes (with no members)
ClassName
Circle
![Page 206: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/206.jpg)
Notes
◼ Class - The rectangle is the icon for the class. The
name of the class is, by convention, a word with an
initial uppercase letter. It appears near the top of the
rectangle. If your class name has more than one word
name, then join the words together and capitalize the
first letter of the every word.
![Page 207: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/207.jpg)
Classes (with attributes and operations)
attr1:type1
attr2:type2=“def”
ClassName
centreX:IntcentreY:Int=0
Circle
attr1:type1
attr2:type2=“def”
ClassName
operation1()operation2(args)operation3() : ret type
centreX:IntcentreY:Int=0
Circle
draw() move(Int X, Int Y)
![Page 208: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/208.jpg)
Notes
◼ Attribute - An attribute is a property of a class. It describes a range of values that the property may hold in objects of the class. A class may have zero or more attributes. A one-word attribute name is written in lowercase letter. If the name consists of more than one word, the words are joined and each word other than the first word begins with an uppercase letter. The list of attribute names begins below a line separating them from the class name.
◼ Operations : An operation is something that a class can do, or that you (or another class) can do to a class. Like an attribute name, an operation's name is written in lowercase letter. If the name consists of more than one word, the words are joined and each word except the first word begins with an uppercase letter. The list of operations begins below a line separating operations from the attributes.
![Page 209: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/209.jpg)
Class Visibility
◼ public level +
◼ protected level #
◼ private level -
- centreX:Int- centreY:Int=0
Circle
+ draw() # move(Int X, Int Y)
![Page 210: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/210.jpg)
Notes
◼ Visibility - Visibility applies to attributes or operations,
and specifies the extent to which other classes can use a
given class's attributes or operations. Three levels of
visibility are possible (last symbols are used in UML
classes to indicate different levels of visibility):
◼ public level - usability extends to other classes +
◼ protected level - usability is open only to classes that
inherit from original class #
◼ private level - only the original class can use the
attribute or operation -
![Page 211: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/211.jpg)
Class Multiplicity
◼ A multiplicity in a class specifies the number of instances (objects) of that class that can exist simultaneously.
◼ Only one Library object is allowed in the system (referred to as a singleton object).
◼ Default multiplicity is 0 or more (if not specified)
Library1
![Page 212: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/212.jpg)
Class - Implementation
public class Circle {
private int centreX, centreY; // centre of the circle
//Methods to return circumference and area
protected double move() {
// Implementation here
}
public double draw() {
// Implementation here
}
}
![Page 213: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/213.jpg)
Objects: Instances of Classes
d1: Department : Department
d1: Department
name = “Sales”deptNo = 1
: Department
name = “Sales”deptNo = 1
(a) (b)
(c) (d)
![Page 214: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/214.jpg)
Notes
◼ Figures (a)-(d) show four possible
representations of objects.
❑ Top –
◼ objectName:ClassName – underlined or
◼ :ClassName – underlined
❑ Bottom –
◼ Attribute names and values.
![Page 215: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/215.jpg)
Classes, Objects, and Packages
◼ Packages are a way of grouping classes into common categories.
◼ Classes and Objects are modeled when showing the package they belong to. Classes and objects will interact with classes and objects of different packages—this is how packages are tied together to create a system.
◼ A package is expressed by appending the name of package and a double colon before the class name in either a class or an object.
PackageName::ClassName
ObjectName:Packagename::ClassName
![Page 216: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/216.jpg)
Class Relationships
◼ Classes can related to each other through
different relationships:
❑ Association (delegation)
❑ Generalization (inheritance)
❑ Realization (interfaces)
❑ Dependency
![Page 217: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/217.jpg)
Association
◼ Association describes a link, a link being a
connection among objects between classes.
◼ Association is shown by a solid line between
classes.
![Page 218: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/218.jpg)
Association - Example
◼ A Person works for a Company.
Person Company
employee employer
works for
Association Name
Role
![Page 219: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/219.jpg)
Association - Properties
◼ Name❑ Name of the association
◼ Role❑ The specific role of the association
◼ Multiplicity❑ Indicates the number of objects that are connected
◼ Type❑ Plain association, aggregation, composition
◼ Direction❑ Direction can also be shown for a association
![Page 220: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/220.jpg)
Notes
Name : “works for” Is the name of the relationship.
Role : Person plays the role employee and the Company
plays the role employer.
Multiplicity : Many employees to one company.
Type : This shows a plain association (normally referred to
as association)
![Page 221: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/221.jpg)
Association - Multiplicity
◼ Multiplicity on association specify properties of the number of links that can exist between instances (objects) of the associated classes.❑ That is, it indicates how many objects of one class
relate to one object of another class. It is indicated by a single number or a range of numbers.
◼ We can add multiplicity on either end of class relationship by simply indicating it next to the class where the relationship enters.
Class1 Class2Association name
multiplicitymultiplicity
![Page 222: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/222.jpg)
Association - Multiplicity
◼ A Student can take up to five Courses.
◼ Student has to be enrolled in at least one course.
◼ Up to 300 students can enroll in a course.
◼ A class should have at least 10 students.
Student Coursetakes10..300 1..5
![Page 223: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/223.jpg)
Association - Multiplicity
◼ A teacher teaches 1 to 3 courses (subjects)
◼ Each course is taught by only one teacher.
◼ A student can take between 1 to 5 courses.
◼ A course can have 10 to 300 students.
Teacher Courseteaches 1..3
Association – Multiplicity
1
Studentstakes
1..5
10..300
![Page 224: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/224.jpg)
Association - Multiplicity
◼ Company can have many employees. An employee can
only work for one company.
Person Companyemployee
works for
employer
1
Association - Multiplicity
*
John: Person
James: Person
Microsoft: Company
![Page 225: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/225.jpg)
Association - Multiplicity
class Company{
Vector employee;
public Company(){
employee = new Vector();
}
public static void addEmployee(Employee emp){
employee.addElement(emp);
}
public static void removeEmployee(Employee emp){
empoyee.removeElement(emp);
}
}
class Employee{
………
}
Association - Implementation
![Page 226: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/226.jpg)
Association - Multiplicity
◼ A Student can take many Courses and many Students
can be enrolled in one Course.
Student Coursetakes* *
Alice: Student
Jill: Student
254: Course
253: Course
![Page 227: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/227.jpg)
Notes
◼ One class can be relate to another in a
❑ One-to-one
❑ One-to-many
❑ One-to-one or more
❑ One-to-zero or one
❑ One-to-a bounded interval (one-to-two through twenty)
❑ One-to-exactly n
❑ One-to-a set of choices (one-to-five or eight)
![Page 228: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/228.jpg)
Notes
◼ Multiplicity can be expressed as,
❑ Exactly one - 1
❑ Zero or one - 0..1
❑ Many - 0..* or *
❑ One or more - 1..*
❑ Exact Number - e.g. 3..4 or 6
❑ Or a complex relationship – e.g. 0..1, 3..4, 6..*would mean any number of objects other than 2 or 5
![Page 229: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/229.jpg)
Association - Self
◼ An association that connects a class to itself
is called a self association.
![Page 230: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/230.jpg)
Association - Self
◼ A Company has Employees.
◼ A single manager is responsible for up to 10 workers.
Employeemanager
workerResponsible
for
1
0..10
![Page 231: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/231.jpg)
Class Relationships
▪ Association
▪ Generalization
▪ Realization
▪ Dependency
![Page 232: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/232.jpg)
Generalization (Inheritance)
◼ Child class is a special case of the parent
class
SuperClass
SubClass1 SubClass2
![Page 233: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/233.jpg)
Generalization (Inheritance) e.g.
Circle
GraphicCircle
![Page 234: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/234.jpg)
Inheritance - Implementation
public class Circle {
}
public class GraphicCircle extends Circle {
}
![Page 235: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/235.jpg)
Abstract Class
Shape
Circle Rectangle
![Page 236: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/236.jpg)
Abstract Methods (Operations)
Shape
Circle Rectangle
draw()
draw() draw()
![Page 237: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/237.jpg)
Abstract class and method Implementation
public abstract class Shape {
public abstract draw(); //declare
without implementation
………}
public class Circle {
public draw(){
…….
}
…..
}
![Page 238: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/238.jpg)
Class Relationships
▪ Association
▪ Generalization
▪ Realization
▪ Dependency
![Page 239: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/239.jpg)
Realization- Interface
<<interface>>
TypeWriter
ctl()
pageDown()
brandName
numOfKeys
Keyboard
keyStroke()
◼ Interface is a set of operation the class
carries out
ctl()
pageDown()
brandName
numOfKeys
Keyboard
TypeWriter
OR
![Page 240: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/240.jpg)
Realization - Implementation
public interface TypeWriter {
void keyStroke()
}
public class KeyBoard implements TypeWriter {
public void keyStroke(){
………
}
}
![Page 241: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/241.jpg)
Class Relationships
▪ Association
▪ Generalization
▪ Realization
▪ Dependency
![Page 242: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/242.jpg)
Dependency
◼ Change in specification of one class can
change the other class. This can happen
when one class is using another class.
Circle
PointMove(p:Point)
![Page 243: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/243.jpg)
◼ Dependency relationship can be used to
show relationships between classes and
objects.
Circle
circleA:Circle
circleB:Circle
![Page 244: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/244.jpg)
Class Diagrams
◼ The UML class diagram consists
of several Classes, connected
with Relationships.
![Page 245: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/245.jpg)
Class Diagram - Example
◼ Draw a class diagram for a information modeling system for a school.❑ School has one or more Departments.
❑ Department offers one or more Subjects.
❑ A particular subject will be offered by only one department.
❑ Department has instructors and instructors can work for one or more departments.
❑ Student can enrol in upto 5 subjects in a School.
❑ Instructors can teach upto 3 subjects.
❑ The same subject can be taught by different instructors.
❑ Students can be enrolled in more than one school.
![Page 246: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/246.jpg)
Class Diagram - Example
❑ School has one or more Departments.
School Departmenthas1 1..*
◼ Department offers one or more Subjects.
◼ A particular subject will be offered by only one department.
Department Subjectoffers1 1..*
![Page 247: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/247.jpg)
Class Diagram - Example
◼ Department has Instructors and instructors can
work for one or more departments.
assigned to1..*
Instructor Department1..*
◼ Student can enrol in upto 5 Subjects.
Student Subjecttakes* 0..5
![Page 248: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/248.jpg)
Class Diagram - Example
❑ Instructors can teach up to 3 subjects.
❑ The same subject can be taught by different
instructors.
Instructor Subjectsteaches
1..*
1..3
![Page 249: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/249.jpg)
Class Diagram - Example
❑ Students can be enrolled in more than one
school.
Student Schoolmember
*
1..*
![Page 250: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/250.jpg)
Class Diagram Example
School Department
Student Subject Instructor
1…*
*
member
* 1..5
attends
1..3 1..*
teaches
1..*
1
1 1..*
has
1..*
1..*
assignedTo
offeres
![Page 251: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/251.jpg)
Object Diagram
◼ Object Diagram shows the
relationship between objects.
◼ Unlike classes objects have a state.
![Page 252: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/252.jpg)
Object Diagram - Example
c1: Company
d2: Departmentd1: Department
name=“Sales” name=”CSSE”
p1: Persont
name=“Rao”
p2: Person
name=“Raj”
manager employee
c1: Company
name=“UniMelb”
![Page 253: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/253.jpg)
Sequence Diagram
◼ Shows how objects communicate with each other over time.❑ That is, sequence diagrams are used to model object
interactions arranged in time sequence and to distribute use case behavior to classes.
❑ They can also be used to illustrate all the paths a particular use case can ultimately produce.
◼ The sequence diagram consists of Active Objects, Messages represented as solid-line arrows, and Time represented as a vertical progression.
![Page 254: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/254.jpg)
Sequence Diagram - Objects
◼ A life line illustrates what is happening to an
object in a chronological fashion.
:Name
Life line
Activation
Object
![Page 255: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/255.jpg)
Sequence Diagram – Time & Messages
◼ Messages are used to illustrate communication between
different active objects of a sequence diagram.
:Name1 :Name2
Message Two
ActorMessage One
![Page 256: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/256.jpg)
Types of Messages
◼ Synchronous (flow interrupt until the message
has completed.
◼ Asynchronous (don’t wait for response)
◼ Flat – no distinction between sysn/async
◼ Return – control flow has returned to the caller.
![Page 257: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/257.jpg)
Sequence Diagram – Compilation
:Compiler Linker
ActorCompile
FileSystem
Load Files
Save OBJ Files
Compile files
Link
Load OBJ files
Link OBJ files
Write EXE file
![Page 258: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/258.jpg)
Branching Flow: flow goes to different objects [if
condition is met]
:Editor FileSystem
Load File
:BinaryViewer :TextViewer
[text file]
[binary file]
![Page 259: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/259.jpg)
Alternative Flow: flow changes to alternative
lifeline branch of the same object
Editor
Actor
Exit App
FileSystem
[delete file]
[save file]
![Page 260: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/260.jpg)
Sequence diagram -example
◼ Use case
❑ Add Subject Use Case to URS (University Record
System):
◼ Scenario
❑ Scenario 1 : Subject gets added successfully.
❑ Scenario 2 : Adding the subject fails because the
subject is already in the database.
![Page 261: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/261.jpg)
System Design Principles
◼ System input can take different forms. E.g.
❑ From a graphical user interface
❑ From a command file
◼ URS system should be designed such that
the functionality can be re-used.
◼ Command reading and functionality
implementation have to be separated.
![Page 262: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/262.jpg)
Reading from a command file - example
class URS{
public static void main(String[] args){
URSDatabase u = new URSDatabase();
//Read command from file;
while ( not end of file) {
u.procCommand(cmd);
//Read next commad;
}
//Close file
}
}
![Page 263: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/263.jpg)
execute()
Sequence Diagram – URS Add Subject
Scenario
u:URSDatabase
procCmd(cmd)
sub1:SubjectSubject(id,name)
<< create >>
parseCommand(cmd)
addSubject(sub1)
{transient}
a:AddSubCmd<< create >>
[if cmdN = ADDSUB]AddSubCmd(u,cmdA)
![Page 264: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/264.jpg)
Creating and Deleting objects
c:Client
:Transaction
p: ODBProxy
setVales(a,d,3,4)
<<destroy>>
<<create>>
committed
setAction(a, d, 0)
{transient}
![Page 265: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/265.jpg)
Collaboration Diagrams
◼ Class diagrams indicates what classes are part of our system, what they offer, how they relate, but they don’t tell us how they communicate.
◼ Collaboration diagrams show (used to model) how objects interact and their roles.
◼ They are very similar to sequence diagrams. Actually they are considered as a cross between class and sequence diagram.
◼ Sequence Diagrams are arranged according to Time.
◼ Collaboration Diagrams represent the structural organization of object.
◼ [Both sequence and collaboration diagrams are called interaction diagrams]
![Page 266: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/266.jpg)
Collaboration Diagram – URS Add Subject
Scenario
u:URSDatabase
<<self>>
a:AddSubCmd
2:[if cmdN = ADDSUB]
AddSubCmd(u,cmdA)
<<local>>
{new}
{transient}
3: execute()
3.2: addSubject(sub1)
1:parseCommand(cmd)
procCmd(cmd)
sub1:Subject
3.1: Subject(id,name){new}
![Page 267: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/267.jpg)
Collaboration Diagram – URS Add Subject
Scenario
u:URSDatabase
<<self>>
1:parseCommand(cmd)
procCommand(cmd)
class URSDatabase{private String cmdN;private String cmdA;private parseCommand(String cmd){
cmdN = ….cmdA = ….
}public procCommand(String cmd){
parseCommand(cmd);}
}
![Page 268: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/268.jpg)
Collaboration Diagram – URS Add Subject
Scenario
u:URSDatabase a:AddSubCmd2: AddSubCmd(u,cmdA)
{new}
{transient}
class URSDatabase{private String cmdN;private String cmdA;
public procCommand(String cmd){parseCommand(cmd);if (cmdN == ADDSUB)
AddSubCmd a = new AddSubCmd(u,cmdA);}
}
![Page 269: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/269.jpg)
Collaboration Diagram – URS Add Subject
Scenario
class abstract Command {protected String cmd;protected URSDatabase u;public abstract void execute();
}
class AddSubCmd extends Command{public AddSubCmd(URSDatabase urs, String cmd){
u = urs;// parse command and set the arguments
}public void execute(){
// implement here}
}
![Page 270: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/270.jpg)
Collaboration Diagram – URS Add Subject
Scenario
u:URSDatabase a:AddSubCmd<<local>>
3: execute()
class URSDatabase{private String cmd;public procCommand(String cmd){
parseCommand(0); if (cmd == ADDSUB){
AddSubcmd a = new AddSubCmd(……);}a.execute();
}}
![Page 271: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/271.jpg)
Collaboration Diagram – URS Add Subject
Scenario
a:AddSubCmd
sub1:Subject
3.1: Subject(id,name)
class AddSubCmd{URSDatabase u;
public execute(){
subject sub1 = new Subject(id,name);
}}
![Page 272: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/272.jpg)
Collaboration Diagram – URS Add Subject
Scenario
u:URSDatabase a:AddSubCmd3.2: addSubject(sub1)
class AddSubCmd{URSDatabse u;public execute(){
subject sub1 = new Subject(……);u.addSubject(sub1);
}}
![Page 273: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/273.jpg)
Collaboration Diagram – URS Add Subject
Scenario
class URSDatabase{private String cmd;private Hashtable subjectHash = new HashTable();public procCommand(String cmd){
parseCommand(0); if (cmd == ADDSUB){
AddSubcmd a = new AddSubCmd(……);}a.execute();
}public addSubject(Subject sub);{
subjectHash.put(sub.getKey(), sub);}
}
![Page 274: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/274.jpg)
URS -High Level Class Diagram
URSDatabase
UniversityMember
AcademicStaff Student
Subject
1
*has
1
*
has
teaches
0..3
1
takes *
0…10
![Page 275: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/275.jpg)
Collaboration Diagrams
◼ Collaborations Diagrams show transient links
that exists between objects.
❑ <<self>> - A message from object to itself
❑ << local>> - A message sent due to the object
begin defined as a local variable in the method.
❑ <<parameter>> - The object reference was sent
as a parameter to the method.
❑ <<global>> The object is global.
![Page 276: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/276.jpg)
Sequence Diagram – Enroll Student for subject
successfully
u:URSDatabase
procCmd(cmd)parseCommand(cmd)
execute()
{transient}
a:AssgSubCmd<< create >>
AssgSubCmd(u,cmdA)
getStudent(id)
return stu
getSubject(subId)
return sub[if stu != NULL and sub != NULL]
stu:Student
addSubject(sub)
![Page 277: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/277.jpg)
Collaboration Diagram – Enroll Student in
Subject Scenario
u:URSDatabase
{new}
a:AssgSubCmd
2:AddSubCmd(u,cmdA)
{transient}
<<local>>
3: execute()
3.1: stu: =getStudent(id)
<<self>>
1:parseCommand()
procCmd(cmd)
stu:Student
3.3: [stu !=NULL and
sub!= NULL]:addSubject(sub){parameter}
3.2: sub: = getSubject(subId)
![Page 278: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/278.jpg)
Collaboration Diagram – Enroll Student in
Subject (subject – implementation)
u:URSDatabase
{new}
a:AssgSubCmd
{transient}
<<local>>
3: execute()
3.1: stu: =getStudent(id)
procCmd(cmd)
stu:Student
3.3: [stu !=NULL and
sub!= NULL]:addSubject(sub){parameter}
3.2: sub: = getSubject(subId)
class AssgSubCmd{private URSDatabase u;public execute(){
Student stu = u.getStudent(id);Subject sub = u.getSubject(subId);if (stu != null && sub != null){
stu.addSubject(sub);}
}}
![Page 279: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/279.jpg)
Sequence Diagram – Enroll Student for subject
- Failure
u:URSDatabase
procCmd(cmd)parseCommand()
execute()
{transient}
a:AssgSubCmd<< create >>
AssgSubCmd(u,cmd)
[if stu != NULL and sub != NULL]
stu:Student
addSubject(sub)
getNumSubjects()
return num
Excp[num >= 10]
return e
![Page 280: Object Oriented Methodology · COBOL Fortran LIPS ... VB.NET C# Python. OO Terms and Concepts o Object and Class o Inheritance, Encapsulation and Polymorphism o Relationship and Interaction](https://reader033.vdocuments.mx/reader033/viewer/2022060220/5f0731a27e708231d41bc600/html5/thumbnails/280.jpg)
Reference
◼ https://www.w3schools.com/java/
◼ http://www.cplusplus.com/doc/tutorial/
◼ Nell Dale, Chip Weems and Mark
Headington. Introduction to java and software
design. ISBN 0-7637-1064-4
◼ ผศ. ธวชชย งามสนตวงศ. การเขยนโปรแกรมเชงวตถ. กรงเทพฯ: สถาบนเทคโนโลยพระจอมเกลาพระนครเหนอ. 2545
◼ รศ.ธรวฒน ประกอบพล. Java OOP. กรงเทพฯ: ซมพลฟาย, 2554.