g6dicp - lecture 22

Post on 25-Feb-2016

28 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

G6DICP - Lecture 22. The Theory of Object Oriented Programming. Programming Methodologies. Unstructured Programming Procedural Programming Modular Programming Object Oriented Programming. Unstructured Programming. - PowerPoint PPT Presentation

TRANSCRIPT

School of Computer Science & Information Technology

G6DICP - Lecture 22G6DICP - Lecture 22

The Theory of The Theory of Object Oriented ProgrammingObject Oriented Programming

2

Programming Methodologies Unstructured ProgrammingUnstructured Programming

Procedural ProgrammingProcedural Programming

Modular ProgrammingModular Programming

Object Oriented ProgrammingObject Oriented Programming

3

Unstructured Programming

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

Programmain program

data

4

Procedural 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 programProcedure

Procedure

5

Modular Programming

Procedures are grouped into modules or libraries.

Program

Procedure1 Procedure2

main program

Procedure3

Module1 Module1

6

Properties 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.

7

Object 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.

8

Object 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

9

Objects 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.

10

Properties 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

11

Encapsulation 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.

12

Classes 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.

13

Inheritance 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.

14

The Taxonomy of Insects (simplified)

Winged Insects Wingless Insects

Insects

Flies Social Insects Butterflies Beetles

Bees Wasps Ants

15

Hierarchical 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

16

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)

17

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)

18

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

firstPoint

secondPoint

thirdPoint

19

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)

20

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!

21

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

22

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

23

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)

24

Multiple 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

25

Polymorphism Although methods are inherited, their behaviour sometimes Although methods are inherited, their behaviour sometimes

needs to be modified at different points in the hierarchy.needs to be modified at different points in the 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.

26

Dynamic 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.

top related