1 / 26

G6DICP - Lecture 22

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

hunter
Download Presentation

G6DICP - Lecture 22

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. G6DICP - Lecture 22 The Theory of Object Oriented Programming

  2. Programming Methodologies • Unstructured Programming • Procedural Programming • Modular Programming • Object Oriented Programming

  3. Unstructured Programming • The main program consists of statements that directly operates on global data (ie data that is always available). Program main 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 program Procedure Procedure

  5. Modular Programming • Procedures are grouped into modules or libraries. Program main program Module1 Module1 Procedure1 Procedure2 Procedure3

  6. Properties of Modular Programming • Programs consist of several (sometimes many) interacting parts. • Libraries can be re-used. • Each module can have its own data, and can manage its own internal state. • Each module exists once in a program, and has a single state. • Management of the state of a module can become complex.

  7. Object Oriented Programming • OO programs consist of a web of interacting objects - each with their own state. • Each object is responsible for initialising and destroying itself correctly. • This addresses the complexities of modular programming. • Objects have other attributes that greatly add to the power of manipulating them.

  8. Object Oriented Languages • Pure OO languages • Smalltalk • Java • Impure OO languages • C++ • PERL • Pascal/Delphi • Mixed paradigm languages • Visual Basic • JavaScript

  9. Objects • The world is full of objects. • OO programming was originally developed for the creation of simulations. • Because programs ultimately interact with the real world, many (the vast majority) of programming problems can be solved by mapping program code to “objects” that mirror real world objects.

  10. 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 • When the compiler can’t determine which method implementation to use in advance the appropriate method is chosen at runtime

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

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

  13. Inheritance • Much of human thought is hierarchical • Hierarchies are trees, with a single overall category at the top, and increasing diversity further down. • Characteristics are inherited down the hierarchy (ie any daughter category will inherit the properties of its parents). • An example is biological taxonomy.

  14. The Taxonomy of Insects (simplified) Insects Winged Insects Wingless Insects Flies Social Insects Butterflies Beetles Bees Wasps Ants

  15. 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 (ie the most simple)- lower levels become more specific. • Once a characteristic is defined all categories below that in the hierarchy inherit that characteristic

  16. 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)

  17. 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)

  18. Objects: Data structures that inherit (3) • Instances of class point (objects) firstPoint secondPoint thirdPoint

  19. 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)

  20. Objects: Data structures that inherit (5) • Methods and variables may be public (ie invoked from anywhere), or private (ie 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). NB Java does not use destructors!

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

  22. 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 ) • length of side - integer

  23. 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 - integerSquare (constructor) • togglePoint (inherited but overridden)

  24. Multiple Inheritance • NB This is not implemented in Java! • It is implemented in C++ • A class may have more than one parent. Point String Drawable String

  25. 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. • Method overloading is a form of polymorphism.

  26. Dynamic Method Binding • Where several possible methods are available (eg 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