OOP - Object Oriented Programming Object Oriented Programming is an approach to programming that was developed to make large programs easier to manage. The approach is based on the fact that one of the ways that we make sense of the actual world is by identifying perceived phenomena as separate objects; we also tend to class similar types of objects together - trees, animals, houses etc. The idea is that it is easier to understand the ‘virtual world’ of a program if it can be organised in a similar way to that in which we are used to making sense of the complexities of the actual world. Objects in OOP are often representations of things that we do perceive in the actual world, but they are also used to represent abstract ideas, including ideas developed in programming. Types of objects in the actual world: tree animal house vehicle person Abstract ideas: point vector surface sphere variable loop
OOP - Object Oriented Programming In programming languages, objects have to be described in code. The descriptions usually use similar syntaxes to other constructs in the language being used. Sometimes the syntax for using oop techniques looks exactly like that used for non-oop syntax. This can be confusing if you are unaware of what is going on. The syntax can vary from language to language, just as it can for loops and conditional statements, but once you understand the fundamental concepts of oop, it is relatively easy to recognise how oop techniques are used in different languages.
OOP - Object Oriented Programming OBJECT Properties (Data) Methods (Procedures) In oop, an object is a collection of related data and procedures. The data elements of an object are called properties. The procedures in an object are called methods.
OOP - Object Oriented Programming An example of an object in a program might be a human in a crowd animation. Or it might be a computer graphics primitive like a point, a cube etc.. For an object to be created it is first described in the declaration of a class. The class acts as a template from which an instance of the class - an object, can then be created. Any number of objects can be created based on the same class.
OOP - Object Oriented Programming e.g. A Virtual Actor Object The actor has various characteristics, e.g. speed, wit, health etc.. These can be represented by variables. The actor can exhibit a number of behaviours, e.g. moving from one place to another, picking things up, interacting with other actors. These would be represented by methods. CLASS Properties speed wit health location Methods move pick up meet kiss
OOP - Object Oriented Programming e.g. A Virtual Actor Object During the game the actor can carry out various methods, changing its variables as appropriate. Everything the actor ‘knows’ is represented by its variables; everything it can do is expressed in its methods.
OOP - Object Oriented Programming Messages Objects interact with each other by sending messages to each other, asking them to carry out their methods. A message consists of: The name of an object, followed by the name of a method that the object can execute, followed by any parameters (additional information needed). The object that initiates a message is called the sender and the one that receives the message is called the receiver.
OOP - Object Oriented Programming Classes A class is a template that defines the methods and variables to be included in a particular type of object. The objects that belong to that class are called instances of the class.
OOP - Object Oriented Programming Inheritance Is a way in which one class of objects can be defined as a special case of a more general class, they automatically inherit the methods and variables of the general class. Special cases of a class are called subclasses of that class; the general class is called the superclass of its special cases.
OOP - Object Oriented Programming Inheritance As well as inheriting the methods and variables of their superclass, subclasses may define their own methods and variables and may override aspects of their inheritence.
OOP - Object Oriented Programming Class hierarchy Classes can be nested to any degree, inheritence automatically accumulates down the levels. The resulting tree-like structure is called a class hierarchy.
OOP - Object Oriented Programming Encapsulation Keeping related data and procedures together is called encapsulation. Encapsulation is an extension of the strategy called information-hiding used in structured programming. In object oriented programming the data inside an object can only be accessed by that object’s methods. Objects can’t read each other’s data directly they can only send each other messages (which may include information about their data).
OOP - Object Oriented Programming Variables and data abstraction Most languages have built-in data types. In many languages it is possible to build new data types (called abstract data types) by combining existing data types in new ways. The object oriented approach treats abstract types as if they were part of the original language.
OOP - Object Oriented Programming Composite Objects Variables can contain other objects. This makes it possible to build more complicated objects out of simpler components. Objects that contain other objects are called composite objects.
OOP - Object Oriented Programming Example messages ogre15.turn(90) Is how a message to an object named ogre15 asking it to use its turn method with a parameter of 90 degrees might look in C++. age + 10 In this case and object named ‘age’ is being asked to use the method ‘+’ with the parameter 10. In this case ‘age’ is an instance of the class ‘number’ and numbers know how to carry out simple arithmetic methods.
OOP - Object Oriented Programming Returned responses A sender may request that the receiver return a message, perhaps a confirmation that the task could be carried out. This response is called a return value.
OOP - Object Oriented Programming Overloading Using a technique called overloading it is possible for different classes to have different methods which share the same name. For example, there may be several classes of monster each of which have a method named ‘attack’; each method can be different, but it can be referred to by the same name. This makes it simpler for objects to send messages to each other, as the same name can be used when sending similar messages to different classes.
OOP - Object Oriented Programming Virtual or Abstract Classes A class that represents a high level object that is used as a parent to other classes but is not used to make an instance is called an abstract or virtual class. An example would be ‘vehicle’ - in which a vehicle describes an abstract class that might have subclasses (such as car, lorry, hovercraft) that would be used to create objects - you would not actually have an instance of the class ‘vehicle’.
OOP - Object Oriented Programming Multiple Inheritance Multiple inheritance allows an object to have more than one superclass. There are however problems of complexity as, for example, the two superclasses may have methods with the same name.
OOP - Object Oriented Programming References Taylor, David A., Object Oriented Technology: A Manager’s Guide, Addison-Wesley, 1990 ISBN: Lafore, Robert., Object-Oriented Programming in Turbo C++, (Chapter 1), Waite Group Press, 1991