Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
From Structured to Object Oriented Programming.

Background

A frequent comment - "I'm very used to conventional / structured programming, but I'm now trying to learn about Object Orientation and I don't understand the idea and I get baffled by all the buzzwords I hear". And a request "Can you help me?"

Yes - I can help you. If you come on a course that introduces Python, Ruby or C++, you'll be taught about Object Orientation and I'll clear the fog for you. With languages such as PHP and Perl, Object Orientation is just one way to write your code, and I cover "OO" on Perl for Larger Projects and on Object Orientation in PHP. With Tcl, Object Orientation comes in the [Incr Tcl] extension, and if I know in advance I can cover it during an extra evening session on out Tcl Courses.

So - learn a new programming language with us, and we'll include Object Oriented principles within the course as appropriate - you'll leave with a thorough grounding in the principles, and how they're applied to the language you have learned.

But - what if you're not learning one of the languages we teach?. Well - I can still be of some (but rather more limited) help in introducing you to Object Orientation - indeed, I did just such a thing for "RA" one evening last week; I'll be happy to do the same for you - in essence, it's half a day's training, and I'll be happy to do it where there's a gap in my schedule.

Basic Detail (the technical stuff)

When you're dealing with data, you want to perform a number of operations on that data. Some things are sensible to do, and others would be rather silly.

Let's take an example - let's say I'm writing a program (or suite of programs - in real life, you usually have a whole load of programs to look after your information) that deals with tables - the things that you sit at in a restaurant, or in class. There are certain operations that you'll want to perform on tables, such as calculating the area, and working out how many guests you can sit at them for food service. And there are certain operations you could perform on the data associated with tables that would be nonsense - for example, you wouldn't want to work out the square root of the length of one of the sides ...

So - what I'm going to do is to write a named block of code (in structured programming terms, you'll be familiar with the name function or subroutine or perhaps procedure or even command or macro) which I can call, passing in my initial data parameters about a table. And I'm going to have that named block of code return something I can store into a variable. Taking Python as my language of demonstration, here's how the calling code may look:

  first = table(2100,900)

No need in Python to declare variable types (or anything line that), but I have already pulled in from a separate file (or coded into the top of the file that contains this call) a definition of what a table is.

Having defined a table, I can then pass it into other named blocks of code which perform calculations on it. So, for example, I can write:

  pt1 = first.getpeople()

which is going to call the piece of code named getpeople on the data that's contained in the variable called first.

Simple idea, isn't it? I can create a whole lot of different tables - after all, a restaurant with just one table isn't going to be particularly cost effective, is it? And within the code that I've not shown you (yet!) that defines what a table is, I can create lots of other named blocks of code that let me do things with that table data. But in each case, I'm only going to have to worry about how the code works the first time I write it - thereafter, I can keep re-using the same code in lots of different programs. That's really nothing different to real life - after all, if you go into a restaurant and ask for a table for four, you're unlikely to quiz the member of staff who seats you on how he knows that the table he takes you to seats four, rather than 3 or 6!

Let's now add some terminology .
• We'll call the code that's all about tables a class
• We'll call each of the functions / subroutines in the class methods
• We'll call each individual data member of the class (each table) an object
• We'll call the method that sets up each object in the first place a constructor method
• We'll call the variable into which we store the object returned by the constructor an instance variable
• We'll call the set of methods that the user can call the API or Application Programmer Interface
• and We'll call the act of hiding logic within methods in the class, so that the application programmer doesn't need to know about it, encapsulation.

Here's a diagram showing how those elements go together - there may be a few more terms on this, as it's the final diagram from all my work the other evening, and it shows not one but three objects.


To complete this first example, and show you a runnable program, I also need to define my class; here's a complete program, in Python, showing both the class and a sample program that uses it all in one:

  class table (object):
    def __init__(self,width,height):
      self.width = width
      self.depth = height
    def getpeople(self):
      edge = int(self.width / 830)
      otheredge = int (self.depth / 830)
      total = 2 * (edge + otheredge)
      return total
  
  # ---------- ABOVE here - definition of a table
  # ---------- BELOW here - example program code to test and use tables
  
  first = table(2100,900)
  second = table(1900,840)
  third = table(1200,1200)
  
  pt1 = first.getpeople()
  pt2 = second.getpeople()
  s3 = third.getpeople()
  
  print "We can seat",pt1,"and",pt2,"and",s3,"people"


I've chosen, for this example, to create several (actually three) table objects, with different dimensions for each of them, to show you how my code is able to keep three different widths and three different heights apart from each other; when I run the code, I get:

  munchkin:oos12 grahamellis$ python d2
  We can seat 6 and 6 and 4 people
  munchkin:oos12 grahamellis$


The logic which defines the table - above the comment lines in my code above - comprises two methods. In Python, the constructor method is known as __init__, and I've used it simply to store two data values. The second method, which I've called getpeople, actually calculates the number of people who can sit at the table on which the method is run, and returns that number. You'll note that the method hides the logic from the calling program below the comment lines; indeed the main application programmer need know nothing about the recommended table frontage required by each diner, nor that fact that diners cannot be wrapped around table corners.

Second Stage (the technical stuff)

Not all tables are equal - but some are more equal that others (Apologies to George Orwell, who in Animal Farm wrote "All animals are equal, but some are more equal than others).

We looked at rectangular tables in the example just above, and we were able to vary the two dimensions to perform calculations for different tables. The algorithms for the calculations were the same in each case, but the numbers - and thus the results - differed. But let's go a step further. We may have a round table. Now - some of the code / algorithms may be the same, but others may differ. One way we could write code for a round table would be to duplicate the code for our original table, and then modify the copy. That would work well initially, but then when we came to update the common sections of the code later, we would find that we had two - and perhaps by that time many more - copies. so we need something better.

Let's define a class of table just as we did before. But we'll then define a roundtable class in such a way that we say "a roundtable is a table but with the following extra methods / methods replaced". That way, we don't need to copy the code - we can just supply (in our roundtable class) a set of changes. And we can go on and do the same thing for a squaretable too, and perhaps for many other classes of table. Indeed, we arrange then in a hierarchy.

This diagram shows how we might build our classes into a tree, and it adds in some of the extra OO keywords that are added to describe the concepts.


Let's now see that extra terminology .
• We'll call a class that other classes are based on a base class.
• Another word for a base class is a parent class.
• We'll call a class that's built from another an extended class.
• and We'll say that an extended class inherits from a base class.
• We may also call our extended class a subclass as it will have fewer object members than the base class.
• Where we replace a method in a base class with alternative code in a subclass, we call it overriding a method.

Here's a complete piece of code in which I've defined three different types of tables

  class table (object):
    def __init__(self,width,height,material=""):
      self.width = width
      self.depth = height
    def getpeople(self):
      edge = int(self.width / 830)
      otheredge = int (self.depth / 830)
      total = 2 * (edge + otheredge)
      return total
  class roundtable (table):
    def __init__(self,diameter,material=""):
      self.diameter = diameter
    def getpeople(self):
      total = int (3.14159265 * self.diameter / 830)
      return total
  class squaretable(table):
    def __init__(self,width,material=""):
      self.width = width
      self.depth = width
  
  # ---------- ABOVE here - definition of a table
  # ---------- BELOW here - example program code to test and use tables
  
  first = table(2100,900)
  second = table(1900,840)
  third = squaretable(1200)
  fourth = roundtable(890)
  clump = [first,second,third,fourth]
  
  for tabby in clump:
    pat = tabby.getpeople()
    print "This table will seat",pat


I've set up a list of tables (of various types) and I've then looped through each of them in turn calling the getpeople method. There are actually two different methods of this name, and as the loop runs, the appropriate one will be run for a roundtable, or for any other sort of table. You'll note that there's no need within your code to tell which is which; provided that you've made available methods of the same name which perform the same logical operation, the language will work out which one it needs to run - even different ones different times round the loop. Gone is the eed for if / elseif / elseif / elseif / elseif / type coding.

One final piece of terminology ...
• The work we use to describe this selection of the appropriate method as the code runs is know as polymorphism.

And looking further ahead

There's a limit to what I should cover in a single lesson on Object Orientation; what I've shown you above covers the basic principles you'll need on day one, but there are many other concepts and terms that you're likely to come across on a course.
• Public, protected, package and private declarations
• Final methods
• Abstract classes
• Class and object / unbound and bound / static and dynamic methods
• Accessors and properties
• Autoloaders
• Destructors
• Serialising and unserialising objects / picking and shelving
• Overloaded methods
• Design Patterns
• Factories, Comparators and Singletons
• Mixins, Multiple Inheritance and Interfaces
These don't all apply in every language, but each has its place and its reason, and all together they make object oriented programming the "power programming" methodology for the future.

(written 2012-10-02, updated 2012-10-13)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q101 - Object Orientation and General technical topics - Programming Principles
  [4206] Writing the perfect program in Tcl? - (2013-11-13)
  [4153] Rooms available tonight - how to code an algorithm from first principles - (2013-08-19)
  [4118] We not only teach PHP and Python - we teach good PHP and Python Practice! - (2013-06-18)
  [4090] Test Driven Development in Python - Customer Comes First - (2013-05-16)
  [4061] Seamless, integrated IT - we have a long way to go! - (2013-04-11)
  [4003] Web and console - same principle, same code - Ruby example - (2013-02-14)
  [3954] Lesson 1 in programing - write clean, reuseable and maintainable tidy code - (2012-12-16)
  [3928] Storing your intermediate data - what format should you you choose? - (2012-11-20)
  [3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
  [3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
  [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
  [3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
  [3456] Stepping stones - early coding, and writing re-usable code quickly - (2011-09-24)
  [3026] Coding efficiency - do not repeat yourself! - (2010-11-02)
  [2964] An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21)
  [2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
  [2878] Program for reliability and efficiency - do not duplicate, but rather share and re-use - (2010-07-19)
  [2769] Easy - but for whom? - (2010-05-18)
  [2737] Improving your function calls (APIs) - General and PHP - (2010-04-24)
  [2586] And and Or illustrated by locks - (2010-01-17)
  [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
  [2510] The music of the stock market - (2009-11-22)
  [2415] Variable names like i and j - why? - (2009-09-22)
  [2327] Planning! - (2009-08-08)
  [2310] Learning to write high quality code in Lua - (2009-07-30)
  [2228] Where do I start when writing a program? - (2009-06-11)
  [2022] Pre and post increment - the ++ operator - (2009-02-03)
  [2001] I have not programmed before, and need to learn - (2009-01-19)

Q907 - Object Orientation and General technical topics - Object Orientation: Design Techniques
  [4098] Using object orientation for non-physical objects - (2013-05-22)
  [3978] Teaching OO - how to avoid lots of window switching early on - (2013-01-17)
  [3887] Inheritance, Composition and Associated objects - when to use which - Python example - (2012-10-10)
  [3844] Rooms ready for guests - each time, every time, thanks to good system design - (2012-08-20)
  [3798] When you should use Object Orientation even in a short program - Python example - (2012-07-06)
  [3763] Spike solutions and refactoring - a Python example - (2012-06-13)
  [3760] Why you should use objects even for short data manipulation programs in Ruby - (2012-06-10)
  [3607] Designing your application - using UML techniques - (2012-02-11)
  [3454] Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24)
  [3260] Ruby - a training example that puts many language elements together to demonstrate the whole - (2011-04-23)
  [3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
  [3063] Comments in and on Perl - a case for extreme OO programming - (2010-11-21)
  [2977] What is a factory method and why use one? - Example in Ruby - (2010-09-30)
  [2953] Turning an exercise into the real thing with extreme programming - (2010-09-11)
  [2889] Should Python classes each be in their own file? - (2010-07-27)
  [2865] Relationships between Java classes - inheritance, packaging and others - (2010-07-10)
  [2785] The Light bulb moment when people see how Object Orientation works in real use - (2010-05-28)
  [2747] Containment, Associative Objects, Inheritance, packages and modules - (2010-04-30)
  [2741] What is a factory? - (2010-04-26)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2523] Plan your application before you start - (2009-12-02)
  [2501] Simples - (2009-11-12)
  [2380] Object Oriented programming - a practical design example - (2009-08-27)
  [2170] Designing a heirarcy of classes - getting inheritance right - (2009-05-11)
  [2169] When should I use OO techniques? - (2009-05-11)
  [1538] Teaching Object Oriented Java with Students and Ice Cream - (2008-02-12)
  [1528] Object Oriented Tcl - (2008-02-02)
  [1435] Object Oriented Programming in Perl - Course - (2007-11-18)
  [1224] Object Relation Mapping (ORM) - (2007-06-09)
  [1217] What are factory and singleton classes? - (2007-06-04)
  [1047] Maintainable code - some positive advice - (2007-01-21)
  [836] Build on what you already have with OO - (2006-08-17)
  [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
  [747] The Fag Packet Design Methodology - (2006-06-06)
  [656] Think about your design even if you don't use full UML - (2006-03-24)
  [534] Design - one name, one action - (2005-12-19)
  [507] Introduction to Object Oriented Programming - (2005-11-27)
  [236] Tapping in on resources - (2005-03-05)
  [80] OO - real benefits - (2004-10-09)

Y106 - Object Oriented Python
  [4129] Simple OO demonstration in C++, comparison to Python - (2013-07-01)
  [4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
  [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
  [3947] this or self - what are they, and what is the difference? (Python) - (2012-12-08)
  [3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
  [2017] Python - a truly dynamic language - (2009-01-30)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [1348] Screw it or Glue it? Access to Object variables - a warning - (2007-09-12)
  [1306] Python class rattling around - (2007-08-16)
  [900] Python - function v method - (2006-10-20)
  [834] Python makes University Challenge - (2006-08-15)
  [477] Class, static and unbound variables - (2005-10-25)


Back to
Public Transport Services - from and to Melksham
Previous and next
or
Horse's mouth home
Forward to
Mixed mode travel - Information systems
Some other Articles
Community Transport - Pewsey, Taunton, and the whole picture too
Wiltshire Public Transport User Group co-ordination
Chamber of Commerce - looking forward in Melksham and in Wessex
Mixed mode travel - Information systems
From Structured to Object Oriented Programming.
Public Transport Services - from and to Melksham
October to December 2012 - Public Courses
Using CGI and Perl to put a simple application online. Sometimes still the best way.
Using Perl to read an RSS feed off a web site and extract data - via LWP and XML modules
Henbury loop, Bristol - a freight railway line with passenger potential?
4300 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3878_-Fr ... ming-.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat