1 / 19

Object Oriented Theory II

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

yardley
Download Presentation

Object Oriented Theory II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 20 Object Oriented Theory II

  2. Programming Methodologies Unstructured Programming Procedural Programming Modular Programming Properties of Modular Programming Object Oriented Programming Object Oriented Languages Objects Previously

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

  4. 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. 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. 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. The Taxonomy of Insects

  8. 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. 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. 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. Objects – Data Structures that Inherit (3) • Instances of class point (objects) Point 1 Point 2 Point 3

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

  15. 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. 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. Multiple Inheritance • N.B. This is not implemented in Java! • It is implemented in C++ • A class may have more than one parent String Point Drawable String

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

More Related