Top Banner
chool of Computer Science & Information Technology G6DICP - Lecture 22 G6DICP - Lecture 22 The Theory of The Theory of Object Oriented Programming Object Oriented Programming
26

School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

Dec 30, 2015

Download

Documents

Hugh Greer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

School of Computer Science & Information Technology

School of Computer Science & Information Technology

G6DICP - Lecture 22G6DICP - Lecture 22

The Theory of The Theory of Object Oriented ProgrammingObject Oriented Programming

Page 2: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

2

Programming MethodologiesProgramming Methodologies Unstructured ProgrammingUnstructured Programming

Procedural ProgrammingProcedural Programming

Modular ProgrammingModular Programming

Object Oriented ProgrammingObject Oriented Programming

Page 3: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

3

Unstructured ProgrammingUnstructured Programming

The main program consists of statements that directly operates on global data (ie data that is always available).

Programmain program

data

Page 4: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

4

Procedural ProgrammingProcedural Programming

Sequences of statements are combined into one place called a procedure. This can then be invoked as often as needed by the main program.

Program

main program

Procedure

Procedure

Page 5: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

5

Modular ProgrammingModular Programming

Procedures are grouped into modules or libraries.

Program

Procedure1 Procedure2

main program

Procedure3

Module1 Module1

Page 6: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

6

Properties of Modular ProgrammingProperties of Modular Programming Programs consist of several (sometimes many) Programs consist of several (sometimes many)

interacting parts.interacting parts. Libraries can be re-used.Libraries can be re-used. Each module can have its own data, and can Each module can have its own data, and can

manage its own internal manage its own internal statestate.. Each module exists once in a program, and has a Each module exists once in a program, and has a

single state.single state. Management of the state of a module can Management of the state of a module can

become complex.become complex.

Page 7: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

7

Object Oriented ProgrammingObject Oriented Programming OO programs consist of a web of interacting OO programs consist of a web of interacting objectsobjects - each with their own state. - each with their own state.

Each object is responsible for initialising and Each object is responsible for initialising and destroying itself correctly.destroying itself correctly.

This addresses the complexities of modular This addresses the complexities of modular programming.programming.

Objects have other attributes that greatly add to Objects have other attributes that greatly add to the power of manipulating them.the power of manipulating them.

Page 8: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

8

Object Oriented LanguagesObject Oriented Languages Pure OO languagesPure OO languages

SmalltalkSmalltalk JavaJava

Impure OO languagesImpure OO languages C++C++ PERLPERL Pascal/DelphiPascal/Delphi

Mixed paradigm languagesMixed paradigm languages Visual BasicVisual Basic JavaScriptJavaScript

Page 9: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

9

ObjectsObjects

The world is full of objects.The world is full of objects. OO programming was originally developed for OO programming was originally developed for

the creation of simulations.the creation of simulations. Because programs ultimately interact with the Because programs ultimately interact with the

real world, many (the vast majority) of real world, many (the vast majority) of programming problems can be solved by programming problems can be solved by mapping program code to “objects” that mirror mapping program code to “objects” that mirror real world objects.real world objects.

Page 10: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

10

Properties of OO ProgrammingProperties of OO Programming EncapsulationEncapsulation

Combining data with the code that acts upon that data to form a Combining data with the code that acts upon that data to form a new data-type - an object.new data-type - an object.

InheritanceInheritance Arranging objects into a hierarchy of descendant objects, with Arranging objects into a hierarchy of descendant objects, with

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

PolymorphismPolymorphism A single action may be used in different ways in different A single action may be used in different ways in different

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

Dynamic method bindingDynamic method binding When the compiler can’t determine which method implementation to When the compiler can’t determine which method implementation to

use in advance the appropriate method is chosen at runtimeuse in advance the appropriate method is chosen at runtime

Page 11: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

11

EncapsulationEncapsulation

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

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

Consider an orange:Consider an orange: Mathematical view - abstracted into separate components Mathematical view - abstracted into separate components

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

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

Page 12: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

12

ClassesClasses

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

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

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

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

Page 13: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

13

InheritanceInheritance

Much of human thought is hierarchicalMuch of human thought is hierarchical Hierarchies are trees, with a single overall Hierarchies are trees, with a single overall

category at the top, and increasing diversity category at the top, and increasing diversity further down.further down.

Characteristics are inherited down the hierarchy Characteristics are inherited down the hierarchy (ie any daughter category will inherit the (ie any daughter category will inherit the properties of its parents).properties of its parents).

An example is biological taxonomy.An example is biological taxonomy.

Page 14: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

14

The Taxonomy of Insects (simplified)The Taxonomy of Insects (simplified)

Winged Insects Wingless Insects

Insects

Flies Social Insects Butterflies Beetles

Bees Wasps Ants

Page 15: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

15

Hierarchical TaxonomyHierarchical Taxonomy

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

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

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

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

Page 16: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

16

Objects: Data structures that inherit (1)

Objects: Data structures that inherit (1) Consider a program that handles graphics.Consider a program that handles graphics. We might define a series of classes to draw shapes on We might define a series of classes to draw shapes on

the screen.the screen. The top level class is The top level class is LocationLocation This represents a position on screenThis represents a position on screen

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

Page 17: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

17

Objects: Data structures that inherit (2)

Objects: Data structures that inherit (2) If we want to display a pixel we can use a If we want to display a pixel we can use a

subclass subclass PointPoint..

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

Page 18: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

18

Objects: Data structures that inherit (3)

Objects: Data structures that inherit (3) Instances of class point (objects)Instances of class point (objects)

firstPoint

secondPoint

thirdPoint

Page 19: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

19

Objects: Data structures that inherit (4)

Objects: Data structures that inherit (4) Classes contain data (X co-ordinate, Y co-Classes contain data (X co-ordinate, Y co-

ordinate and visible), encapsulated with code ordinate and visible), encapsulated with code that operates on that data.that operates on that data.

A method called drawPointA method called drawPoint

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

Page 20: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

20

Objects: Data structures that inherit (5)

Objects: Data structures that inherit (5)

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

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

A class may have a destructor (a method that is automatically A class may have a destructor (a method that is automatically invoked when an object is destroyed). invoked when an object is destroyed). NB Java does not use NB Java does not use destructors!destructors!

Page 21: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

21

Objects: Data structures that inherit (6)

Objects: Data structures that inherit (6)

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

Page 22: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

22

Objects: Data structures that inherit (7)

Objects: Data structures that inherit (7) Point may be subclassed as Circle or SquarePoint may be subclassed as Circle or Square

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

Square (subclass of Point)Square (subclass of Point) ( inherited - X co-ordinate )( inherited - X co-ordinate ) ( inherited - Y co-ordinate )( inherited - Y co-ordinate ) ( inherited - visible )( inherited - visible ) length of side - integerlength of side - integer

Page 23: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

23

Objects: Data structures that inherit (8)

Objects: Data structures that inherit (8)

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

Square (subclass of Point)Square (subclass of Point) ( inherited - X co-ordinate )( inherited - X co-ordinate ) ( inherited - Y co-ordinate )( inherited - Y co-ordinate ) ( inherited - visible )( inherited - visible ) length of side - integerlength of side - integer

Square (constructor)Square (constructor) togglePoint (inherited but overridden)togglePoint (inherited but overridden)

Page 24: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

24

Multiple InheritanceMultiple Inheritance NB This is not implemented in Java!NB This is not implemented in Java! It It isis implemented in C++ implemented in C++ A class may have more than one parent.A class may have more than one parent.

Point String

Drawable String

Page 25: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

25

PolymorphismPolymorphism

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

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

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

Method overloading is a form of polymorphism.Method overloading is a form of polymorphism.

Page 26: School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.

26

Dynamic Method BindingDynamic Method Binding

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

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

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