Sunday, December 23, 2012

Why Object-Oriented Programming?

OOPs!

Object-oriented programming (OOP) is a style of computer programming that emphasizes the creation and use of software objects to write programs.  You can think of a software object as a model, in software, of a thing or concept.  A software object provides a representation (in software) of the characteristics or attributes of the thing/concept being modeled along with a definition of the things it can do.  The attributes (aka its properties) that define an object are typically things the object has or is; for example if you were modeling a person the set of attributes would include height and weight.  The things an object can do (aka its behaviors), are generally the actions that it can perform (for a person object this could be things like run, jump, speak, etc.).  Now before we go any further let’s try to clarify this with an example; we are going to develop an object-oriented model of an atom (e.g., hydrogen, oxygen, etc.).

Now a very simplified model of an atom might include the following properties:
  • Protons (number of protons of the atom)
  • Neutrons (number of neutrons of the atom)
  • Electrons (number of electrons of the atom)
We'll also include some of the things that we can do with a software model of an atom (its behaviors):
  • Get Element (determine the chemical element name [e.g. hydrogen, helium, etc.]  of the atom).
  • Get Mass (determine the atomic mass of the atom).
  • Illustrate (display a diagram of the atom).
  • Perform Fission (split the nucleus – very dangerous, perform at your own risk!!).
  • Create (create an Atom object with the desired number of protons, neutrons, and electrons). 
The resulting object-oriented software model for an atom consists of these properties and behaviors.
Atom Object

OK, now we have a software model for an atom, so what’s next?  Well, you get a software object to perform an operation (i.e. a behavior) by sending a message to it with the operation you want performed.  For example, if you send the message Get Element to an atom object it will determine the chemical element name for the atom and return that information as text (e.g., it might return the text “Hydrogen” or “Helium”).  Object-oriented software design is all about creating software objects of this sort and using them to build software applications.  Programmers create networks of interacting objects to implement all sorts of applications.  

Now there are literally thousands of programming languages, and an OOP language is just one of many options for writing programs.  So why build this software using objects, why not use another approach?  Well, compared to other programming styles, OOP has a variety of benefits and here are just a few of them:
  • OOP is a natural tool for modeling things in software, whether they are real-world entities, abstract concepts, and/or processes.  This can reduce complexity and make the program structure more clear. 
  • Because you can create software objects that completely define the properties (i.e. states) and behavior of a thing being modeled, they can be reused in a variety of ways.  You can use them to build things that are related and descend from a common parent (in OOP this technique is called inheritance), or things that are built up from other objects (this technique is called composition).
  • It's easy to make internal changes to an object, as it is self-contained.  When an object is designed properly, changes to its implementation do not affect any other part of a program, since the interface that the external world has to the object is specified by its operations (i.e. methods).
  • Object-oriented software is extensible, meaning that making updates or adding new features doesn’t require a complete rewrite of a program.  Often, you can update object-oriented software by introducing a few new objects and/or modifying some existing ones. 
  • As well-defined objects are self-contained and used through a published interface, they can be maintained separately.  This makes maintaining programs easier, along with locating and fixing problems.  For large programs this feature is especially important.
To put OOP in perspective, much of the software that you use today (e.g. programs that you use on your computers, mobile devices, websites, or in machines) was written using an OOP language.  For example, iPhone, iPod, and iPad applications are primarily written in Objective-C.  Much of the user interface for Facebook is written with PHP, a programming language that supports OOP.  Android applications for your mobile device are written in Java, an OOP language; in fact, Java is used in over 3 billion devices.  Many Windows applications are written in Visual Basic or C#, both of which provide extensive support for OOP.

In summary, OOP is one of the most common programming paradigms in use today and is supported by many programming languages.  If you want to learn more about OOP there are plenty of books and online resources available.  So what are you waiting for, dive in and start programming using OOP!

Programming for Everyone is designed to give the reader an introduction to computer programming. The book is written for a very general audience and focuses on providing you with a detailed understanding of the basic concepts.  It's also great for programmers who want to learn about other programming areas (e.g. logic programming, computer graphics, games, etc.) they may not have experience in.  Whatever your age or background, Programming for Everyone will help you to understand computer programming!
 
  

No comments:

Post a Comment