OO (Object Oriented) Programming
Chapter 21
A method of Design, (make the major decisions first when designing programs)
OO object oriented programs“Balloon” as an example, a simulation of a balloonPrincipals of OOP1. Encapsulation (Chapter 9)2. Inheritance (Chapter 11)3. Polymorphism
Encapsulation
…group related information together and protected from the outside (world)Private = protected orOpen = open to the world
Inheritance
Use classes repeatedlyReuse parts of existing classes when possible
Polymorphism
Same methods used with others, multiple implementations
Design problems
Need to identify objects. An approach to problem solvingSimulation and Modeling Build a software model of something in the
real world Identify the object(s) in the problem Model them as objects Ignore irrelevant detail
Specification = what is requiredDesign = how it will be accomplished
Model – view - controller
An example:A cars engine (model) the observer = you gas pedal (controller) speedometer (some dial or gage) i.e. GUI
What user adjusts with (a scrollbar (controller) as an example)
A Balloon example
Model - car engine vs. BalloonView – speedometer vs. CircleController – steering wheel vs. change position of Balloon (left/right, up/down)
Hierarchy
An old example “Sphere” class as the parent and Orb, or Circle, or marble or whatever as child.Many other sub classes possibleAnalyze the specifications of what you are to model (design)Write in pseudocode before using Java
Case study in design
“One Armed Bandit” example “Slotmachine”: “Actions”DisplayGoQueryStop Reuse the components that your program can inherit
Relationships
“is a” vs. “has a”Is a = inheritance (java “extends”) from parent classHas a = (java “new”) an objectNeeds a = utility (java various methods) need of another object, may pass in a parameter)
Reuse components that your program can inherit
Top Related