What will we cover?
- What is an object?
- What is a Class?
- What are polymorphism and inheritance?
- Creating, Storing and using objects
What is Object Oriented Programming?Now we move onto what might have been termed an advancedtopic up until about 10 years ago. Nowadays '
Object OrientedProgramming has become the norm. Languages like Java andPython embody the concept so much that you can do very littlewithout coming across objects somewhere. So what's it all about?The best introductions are, in my opinion:
- Object Oriented Analysis by Peter Coad & Ed Yourdon.
- Object Oriented Analysis and Design with Applicationsby Grady Booch (the 1st edition if you can find it)
- Object Oriented Software Construction by BertrandMeyer (definitely the 2nd edition of this one)
These increase in depth, size and academic exactitude as you godown the list. For most non-professional programmers' purposes the first is adequate. For a more programming focused introduction try
Object Oriented Programming by Timothy Budd(2nd edition).This uses several languages to illustrate object oriented programming techniques. It is much more strongly oriented towards writing programs than any of the other books which cover the whole gamut of theory and principle behind object orientation, at the design level as well as at the code level.Finally for a whole heap of info on all topics OO try the Weblink site at: -links.orgAssuming you don't have the time nor the inclination to researchall these books and links right now, I'll give you a briefoverview of the concept. (
Note:Some people find OO hard to graspothers 'get it' right away. Don't worry if you come under theformer category, you can still use objects even without really'seeing the light'.)One final point: it is possible to implement an Object Oriented design in a non OO language through coding conventions, but it's usually an option of last resort rather than a recommended strategy. If your problem fits well with OO techniques then it's best to use an OO language. Most modern languages, including Python, VBScript and JavaScript support OOP quite well. That having been said I will be using Python throughout all the examples and only showing the basic concepts in VBScript and JavaScript with little additional explanation.Data and Function - togetherObjects are collections of data and functions that operate onthat data. These are bound together so that you can pass anobject from one part of your program and they automatically getaccess to not only the data
attributes but the
operations that are available too. This combining of data and function is the very essence of Object Oriented Programming and is known as
encapsulation. (Some programming languagesmake the data invisible to users of the object and thus require that the data be accessed via the object's methods. This technique is properly known as
data hiding, however in some texts data hiding and encapsulation are used interchangeably.)As an example of encapsulation, a string object would store the character string but also provide
methods to operate on that string - search, change case, calculate length etc. Objects use a
message passing metaphor whereby oneobject passes a message to another object and the receivingobject responds by executing one of its operations, a
method. So a method is
invoked on receipt of the correspondingmessage by the owning object. There are various notations usedto represent this but the most common mimics the access toitems in modules - a dot. Thus, for a fictitious widgetclass:w = Widget() # create new instance, w, of widgetw.paint() # send the message 'paint' to itThis would cause the paint method of the widget object to beinvoked.Defining ClassesJust as data has various types so objects can have differenttypes. These collections of objects with identicalcharacteristics are collectively known as a
class. We candefine classes and create
instances of them, which are the actual objects. We can store references tothese objects in variables in our programs.Let's look at a concrete example to see if we can explain itbetter. We will create a message class that contains a string -the message text - and a method to print the message.class Message: def __init__(self, aString): self.text = aString def printIt(self): print( self.text )
Note 1:One of the methods of this class is called __init__ and it is a special method called a
constructor. The reason for the name is that it is calledwhen a new object instance is created or constructed. Anyvariables assigned (and hence created in Python) inside thismethod will be unique to the new instance. There are a number ofspecial methods like this in Python, nearly all distinguishedby the __xxx__ naming format. The exact timing of when a constructor is called varies between languages, in Python init gets called after the instance has actually been created in memory, in other languages the constructor actually returms the instance itself. The difference is sufficiently subtle that you don't usually need to worry about it.
Note 2:Both the methods defined have a first parameterself. The name is a convention but it indicates theobject instance. As we will soon see this parameter is filled in bythe interpreter at run-time, not by the programmer. Thus printIt is called, on an instance of the class (see below), with no arguments: m.printIt().
Note 3:We called the class Message with acapital 'M'. This is purely convention, but it is fairly widelyused, not just in Python but in other OO languages too. A relatedconvention says that method names should begin with a lowercaseletter and subsequent words in the name begin with uppercaseletters. Thus a method called "calculate current balance" wouldbe written: calculateCurrentBalance.You may want to briefly revisit the 'Raw Materials'section and look again at 'user defined types'. The Python address example should be a little clearer now. Essentially the only kind of user-defined type in Python is a class. A class with attributes but no methods (except __init__ ) is effectively equivalent to a construct called a record or struct in some programming languages..
- Ability to solve algorithmic problems of middling difficulty and based on a clear specification, and to implement solutions in an imperative programming language.
- Familiarisation with the basic mechanisms for structuring programmes (modularisation, encapsulation, abstract data types, classes) and ability to apply them to small to medium-sized problems (i.e. comprising few modules).
- Knowledge of object-oriented programme elements (classes, objects, inheritance, execution methods).
- Familiarity with an imperative language, preferably of an object-oriented nature.
- Ability to use and programme simple data structures (tables, linear structures, dictionaries, etc.) in this language.
- Ability to use libraries in this language.
- Mastery of basic strategies for finding and correcting errors in simple modules.
aa06259810