object oriented theory ii

19
1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 20 Object Oriented Theory II

Upload: yardley

Post on 14-Jan-2016

28 views

Category:

Documents


1 download

DESCRIPTION

20. Object Oriented Theory II. Programming Methodologies Unstructured Programming Procedural Programming Modular Programming Properties of Modular Programming Object Oriented Programming Object Oriented Languages Objects. Previously. Properties of OO Programming Encapsulation Classes - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Object Oriented Theory II

1

G54PRG ProgrammingLecture

1

Amadeo Ascó Adam Moore

20

Object Oriented Theory II

Page 2: Object Oriented Theory II

2Amadeo Ascó , Adam Moore

Previously

• Programming Methodologies– Unstructured Programming– Procedural Programming– Modular Programming

• Properties of Modular Programming

– Object Oriented Programming• Object Oriented Languages• Objects

Page 3: Object Oriented Theory II

3Amadeo Ascó , Adam Moore

Overview

• Properties of OO Programming• Encapsulation• Classes• The Taxonomy of Insects• Hierarchical Taxonomy• Objects – Data Structures that Inherit• Multiple Inheritance• Polymorphism• Dynamic Method Binding

Page 4: Object Oriented Theory II

4Amadeo Ascó , Adam Moore

Properties of OO Programming

• Encapsulation– Combining data with the code that acts upon that data to form a new

data-type - an object.

• Inheritance– Arranging objects into a hierarchy of descendant objects, with each

descendant inheriting access to all of its ancestors code and data.

• Polymorphism– A single action may be used in different ways in different contexts –

the implementation of that action being appropriate to the current usage.

• Dynamic method binding

Page 5: Object Oriented Theory II

5Amadeo Ascó , Adam Moore

Encapsulation

• Objects model the real world - they are the ultimate form of data abstraction.

• Encapsulation means keeping all of the constituents of an object in the same place.

• Consider an orange:– Mathematical view - abstracted into separate components (area of

skin, weight, fluid volume, number of seeds etc).– Painters view - encapsulated on canvas an abstract whole.

• Encapsulation ensures that the relationships between the components of an object are preserved.

Page 6: Object Oriented Theory II

6Amadeo Ascó , Adam Moore

Classes

• In most OO languages encapsulation is implemented by the class.

• Java, C++, Object Pascal, and many other programming languages implement OO in this way.

• Classes are user-defined data types that encapsulate code (methods) together with data (variables).

• Each object is a separate instance of a class, and therefore has its own state.

Page 7: Object Oriented Theory II

7Amadeo Ascó , Adam Moore

The Taxonomy of Insects

F lie s

B e es W a sps A n ts

S o c ia l In se c ts B u tte rf lies B e e tles

W in ge d Inse c ts W in g le ss Inse c ts

In se c ts

Page 8: Object Oriented Theory II

8Amadeo Ascó , Adam Moore

Hierarchical Taxonomy

• Consider:– How similar is an item to the others of its general class?– In what ways does it differ from them?

• Each category has a set of behaviours and characteristics that define it.

• The highest levels are the most general (i.e. the most simple)- lower levels become more specific.

• Once a characteristic is defined all categories below that in the hierarchy inherit that characteristic

Page 9: Object Oriented Theory II

9Amadeo Ascó , Adam Moore

Objects – Data Structures that Inherit (1)

• Consider a program that handles graphics.• We might define a series of classes to draw

shapes on the screen.• The top level class is Location• This represents a position on screen

Location X co-ordinate (integer) Y co-ordinate (integer)

Page 10: Object Oriented Theory II

10Amadeo Ascó , Adam Moore

Objects – Data Structures that Inherit (2)

• If we want to display a pixel we can use a Subclass, Point

Point (subclass of Location) ( inherited -X co-ordinate ) ( inherited -Y co-ordinate ) visible (boolean)

Page 11: Object Oriented Theory II

11Amadeo Ascó , Adam Moore

Objects – Data Structures that Inherit (3)

• Instances of class point (objects)

Point 1

Point 3

Point 2

Page 12: Object Oriented Theory II

12Amadeo Ascó , Adam Moore

Objects – Data Structures that Inherit (4)

• Classes contain data (X co-ordinate, Y co-ordinate and visible), encapsulated with code that operates on that data.

• A method called drawPoint

Point (subclass of Location) ( inherited -X co-ordinate ) ( inherited -Y co-ordinate ) visible (boolean) drawPoint (method)

Page 13: Object Oriented Theory II

13Amadeo Ascó , Adam Moore

Objects – Data Structures that Inherit (5)

• Methods and variables may be public (i.e. invoked from anywhere), or private (i.e. only invoked from other methods in the class)

• A class may have a constructor (a method that is automatically invoked when an instance of the class is created).

• A class may have a destructor (a method that is automatically invoked when an object is destroyed).

• N.B. Java does not use destructors!

Page 14: Object Oriented Theory II

14Amadeo Ascó , Adam Moore

Objects – Data Structures that Inherit (6)

Point (subclass of Location) public ( inherited -X co-ordinate ) public ( inherited -Y co-ordinate ) public visible (boolean) private drawPoint (method) private deletePoint (method) public Point (constructor) calls drawPoint public togglePoint calls drawPoint or deletePoint

Page 15: Object Oriented Theory II

15Amadeo Ascó , Adam Moore

Objects – Data Structures that Inherit (7)

• Point may be subclassed as Circle or Square

Circle (subclass of Point) ( inherited -X co-ordinate ) ( inherited -Y co-ordinate ) ( inherited -visible ) radius -integer

Square (subclass of Point) ( inherited -X co-ordinate ) ( inherited -Y co-ordinate ) ( inherited -visible )

Page 16: Object Oriented Theory II

16Amadeo Ascó , Adam Moore

Objects – Data Structures that Inherit (8)

Circle (subclass of Point) ( inherited -X co-ordinate ) ( inherited -Y co-ordinate ) ( inherited -visible ) radius integer Circle (constructor) togglePoint (inherited but overridden)

Square (subclass of Point) ( inherited -X co-ordinate ) ( inherited -Y co-ordinate ) ( inherited -visible ) length of side integer Square (constructor) togglePoint (inherited but overridden)

Page 17: Object Oriented Theory II

17Amadeo Ascó , Adam Moore

Multiple Inheritance

• N.B. This is not implemented in Java!• It is implemented in C++• A class may have more than one parent

String

Drawable String

Point

Page 18: Object Oriented Theory II

18Amadeo Ascó , Adam Moore

Polymorphism

• Although methods are inherited, their behaviour sometimes needs to be modified at different points in the hierarchy.

• The behaviour must be appropriate for the context of use.

• For example - the X,Y coordinates of location could be absolute pixel values or percentages of the screen. A polymorphic method would implement the appropriate functionality.

Page 19: Object Oriented Theory II

19Amadeo Ascó , Adam Moore

Dynamic Method Binding

• Where several possible methods are available (e.g. polymorphic methods) the appropriate method does not need to be indicated to the compiler.

• The decision as to which method to use is made at runtime.

• In Java, this means that the VM selects the correct method to use at runtime.