Download presentation
Presentation is loading. Please wait.
Published byRosalyn Williamson Modified over 9 years ago
1
1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 20 Object Oriented Theory II
2
2 Amadeo Ascó, Adam Moore Previously Programming Methodologies – Unstructured Programming – Procedural Programming – Modular Programming Properties of Modular Programming – Object Oriented Programming Object Oriented Languages Objects
3
3 Amadeo 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
4
4 Amadeo 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
5
5 Amadeo 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.
6
6 Amadeo 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.
7
7 Amadeo Ascó, Adam Moore The Taxonomy of Insects
8
8 Amadeo 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
9
9 Amadeo 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)
10
10 Amadeo 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)
11
11 Amadeo Ascó, Adam Moore Objects – Data Structures that Inherit (3) Instances of class point (objects) Point 1 Point 3 Point 2
12
12 Amadeo 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)
13
13 Amadeo 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!
14
14 Amadeo 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 togglePointcalls drawPoint or deletePoint
15
15 Amadeo 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 )
16
16 Amadeo 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)
17
17 Amadeo 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
18
18 Amadeo 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.
19
19 Amadeo 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.