Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming."— Presentation transcript:

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

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

3 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 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

5 5 Modular Programming Procedures are grouped into modules or libraries. Program Procedure 1 Procedure 2 main program Procedure 3 Module 1

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

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

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

9 9 Objects The world is full of objects. The world is full of objects. OO programming was originally developed for the creation of simulations. 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. 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 10 Properties of OO Programming Encapsulation Encapsulation Combining data with the code that acts upon that data to form a new data-type - an object. Combining data with the code that acts upon that data to form a new data-type - an object. Inheritance Inheritance Arranging objects into a hierarchy of descendant objects, with each descendant inheriting access to all of its ancestors code and data. Arranging objects into a hierarchy of descendant objects, with each descendant inheriting access to all of its ancestors code and data. Polymorphism Polymorphism A single action may be used in different ways in different contexts - the implementation of that action being appropriate to the current usage. 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 Dynamic method binding When the compiler can’t determine which method implementation to use in advance the appropriate method is chosen at runtime When the compiler can’t determine which method implementation to use in advance the appropriate method is chosen at runtime

11 11 Encapsulation Objects model the real world - they are the ultimate form of data abstraction. 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. Encapsulation means keeping all of the constituents of an object in the same place. Consider an orange: Consider an orange: Mathematical view - abstracted into separate components (area of skin, weight, fluid volume, number of seeds etc). Mathematical view - abstracted into separate components (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 the components of an object are preserved. Encapsulation ensures that the relationships between the components of an object are preserved.

12 12 Classes In most OO languages encapsulation is implemented by the class. In most OO languages encapsulation is implemented by the class. Java, C++, Object Pascal, and many other programming languages implement OO in this way. 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). 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. Each object is a separate instance of a class, and therefore has its own state.

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

14 14 The Taxonomy of Insects (simplified) Winged InsectsWingless Insects Insects FliesSocial InsectsButterfliesBeetles BeesWaspsAnts

15 15 Hierarchical Taxonomy Consider Consider 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 characteristics that define it. 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. 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 Once a characteristic is defined all categories below that in the hierarchy inherit that characteristic

16 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 the screen. We might define a series of classes to draw shapes on the screen. The top level class is Location The top level class is Location This represents a position on screen This represents a position on screen  Location  X co-ordinate (integer)  Y co-ordinate (integer)

17 17 Objects: Data structures that inherit (2) If we want to display a pixel we can use a subclass Point. 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 18 Objects: Data structures that inherit (3) Instances of class point (objects) Instances of class point (objects) firstPoint secondPoint thirdPoint

19 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. Classes contain data (X co-ordinate, Y co- ordinate and visible), encapsulated with code that operates on that data. A method called drawPoint A method called drawPoint  Point (subclass of Location)  ( inherited - X co-ordinate )  ( inherited - Y co-ordinate )  visible (boolean)  drawPoint (method)

20 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) 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 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! A class may have a destructor (a method that is automatically invoked when an object is destroyed). NB Java does not use destructors!

21 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 22 Objects: Data structures that inherit (7) Point may be subclassed as Circle or Square 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 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 - integer Square (constructor)  togglePoint (inherited but overridden)

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

25 25 Polymorphism Although methods are inherited, their behaviour sometimes needs to be modified at different points in the hierarchy. 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. 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. 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. Method overloading is a form of polymorphism.

26 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. 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. 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. In Java, this means that the VM selects the correct method to use at runtime.


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

Similar presentations


Ads by Google