cmpt225objects - Object Oriented Programming Encapsulation...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Object Oriented Programming
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Encapsulation Color Class Designing Classes Inheritance Polymorphism September 2004 John Edgar 2
Background image of page 2
Let's say we need to represent colours There are many different colour models One such is the RGB (red green blue) model RGB colours A colour is represented by three numbers, which represent the amount of red, green and blue These values are sometimes recorded as doubles (between 0.0 and 1.0) or sometimes as Integers, between 0 and 255 (or some other number) How many colours can be represented? September 2004 John Edgar 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
September 2004 John Edgar 4 255,0,0 0,0,255 0,255,0 0,0,0 255,128,0 255,255,255 128,128,128 128,128,192
Background image of page 4
We need three variables to represent one colour It would be convenient to refer to colours in the same way we refer to primitive types Object Oriented Programming (OOP) organizes programs to collect variables and methods A class is a factory (or blueprint) for creating objects of a particular type An object is a collection of variables and methods, and is an instantiation of a class Color c = new Color(); September 2004 John Edgar 5 class object constructor
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
An object combines both variables and methods in the same construct Variables give the structure of an object Methods dictate its behaviour A class should be a cohesive construct that performs one task (or set of related tasks) well Objects can be used as if they were primitive types To encapsulate means to encase or enclose Each object should protect and manage its own information, hiding the inner details Objects should interact with the rest of the system only through a specific set of methods (its public interface) September 2004 John Edgar 6
Background image of page 6
The class describes the data and operation s For colours these include: Attributes for red, green and blue Methods to access and change and create colours An individual object is an instance of a class Similar to the way that a variable is of a type Each object has its own space in memory, and therefore each object has its own state Individual Color objects represent individual colours, each with their own values for red, green and blue September 2004 John Edgar 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Using objects can aid design People are used to the idea of everyday objects having states and behaviours This idea can be applied to components of a program OOP also promotes modular design where modules are: Highly cohesive and Loosely coupled September 2004 John Edgar 8
Background image of page 8
To achieve loose coupling, classes are only allowed to communicate through their interfaces Thereby hiding their implementations details Loose coupling is desirable as it: Decreases the chance that changing one module's implementation causes changes to other modules Prevents other modules from assigning invalid values to attributes Information hiding is relatively easy to achieve using object oriented programming September 2004 John Edgar 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 51

cmpt225objects - Object Oriented Programming Encapsulation...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online