Object Oriented Programming
Object Oriented Programming is a Programming Paradigm in which the a program is conceptualised as a network of interconnected objects. Each object is a structure combining data and functionality into a single entity. This is distinct from Procedural Programming in which programs are conceptualised as consisting of data structures and functionality (implemented within Functions and Procedures).
Overviewabstractions. The abstractions uses depends upon the programming paradigm used. There are a number of programming paradigms including structured programming, procedural programming, functional programming, object oriented programming, and others. Each paradigm provides developers with a view of how software should be constructed and a set of abstractions needed to support development using this view.
Compared with Procedural Programming, Object oriented programming paradigm take a higher level view of programs by introducing a new abstraction called objects. Programs are not seen as a hierarchy of routines but rather as a collection of these objects with each performing some role in providing the required functionality. Objects are central to understand how to develop software using this approach, as the name suggests. So what exactly is an object?
What is an object?
Conceptually an object is a piece of programming machinery, a part of the overall solution. These parts (objects) interact by sending messages to each other requesting information and actions. Each object has its own identity and can be thought of as knowing information and being able to perform actions: object know and can do things.
Identify means that each object is unique, it has an identity. If you create two objects they are distinct from each other, and remain so throughout the execution of the program. When we think of objects, we picture each as an individual. For example, if you have a program containing Water Tanks you can create two Water Tank objects and each will have its own identity, associated state and behaviour.
Object’s know things: Each object can contain a number of values in a similar way to a record or other structure type in a procedural programming language. These values are collectively known as the object’s state, and can be thought of as the information that the object knows. In a programming language this state information is stored in fields (also know as instance variables). When the object receives a message it can use the data within its fields to formulate its response. With our Water Tank example, you can imagine that each Water Tank object will knows its capacity and current volume. When a Water Tank object receives a message asking for its current volume it can respond with the value stored in its current volume field. Similarly, if a Water Tank object receives a message asking for its remaining capacity it can read the values stored in its capacity and current volume fields to determine its response. In this way it appears that the Water Tank object knows its capacity, current volume, and remaining volume.
Object’s can do things. This is known as the object’s behaviour. When an object receives a message it can interact with its fields altering the values stored, in effect changing the object's state. Consider the Water Tank object, when it receives an message telling it to empty itself it can achieve this by setting its current volume to 0. After emptying the tank any subsequent message asking for its volume will indicate that the tank is empty. In a programming language this behaviour is implemented as methods. Each method is similar to a function or a procedure in a procedural program, but differs as it is associated with the object it is executed on. Sending a message to an object is achieved in the programming language by executing a method on the object.
Object oriented programs are not usually constructed with a single all knowing and all doing object, but rather they are constructed by distributing functionality across a number of different objects. Software design in the object oriented paradigm involves defining roles for these different objects to play. Each role is then responsible for fulfilling part of the applications functionality, with the role defining the messages that objects of that type can respond to and the actions the objects perform when they receive these messages. The role will have a number of messages for each piece of functionality it is responsible for.
Responsibility driven design
There are a number of different ways to approach designing object oriented software solutions. The responsibility driven design approach uses responsibilities as its central focus. The core concepts of responsibility driven design are roles, responsibilities, and collaborations. Objects in these designs are created to play one (or many) roles. With each role having an associated number of responsibilities that define the information that the objects playing the role will know, and the actions they will be able to perform. Collaboration is then the term used to describe the fact that objects work together to achieve their goals. Objects will have relationships with other objects in the program and can request information and actions from these objects by sending them messages.
Objects and Code
Above we discussed the difference between programming with the object oriented paradigm and programming with the procedural paradigm. Object oriented programming moves the focus from functional decomposition and implementing functions and procedures, to identifying and describing objects. To take full advantage of the object oriented paradigm you need a programming language that provides the ability to describe and work with objects.
Object oriented programming languages offer features that allow you to design and use objects. Many of these languages use an approach in which the developer describes the objects they want to be able to work with using classes. Each class is a template structure used to describe the common characteristics of all its instances (all objects created from the class). Conceptually, a class is an abstraction that encapsulates these common characteristics into a conceptual unit . Objects are instances of a class, each having its own identity and exhibiting the characteristics described by the class it was created from.
These details are discussed further in Objects and Classes.
Principles of Object Oriented Programming
- Abstraction - details of a subject can be factored out to focus on relevant details.
- Encapsulation - (and Information Hiding) relate to combining code and data within objects.
- Inheritance - new classes can be derived from existing classes by modifying or extending the inherited class.
- Polymorphism - (with Dynamic Binding) allowing a message to be passed to many types of objects with potentially different results.
Closely associated with these core principles are the following concepts:
- Roles - Objects perform one or more roles in a solution, with each role defining a set of responsible for its objects.
- Responsibilities - Objects are responsible for knowing and being able to do things.
- Collaboration and Relationships - objects work together to create a solution.
- Collections - classes used to work with multiple objects.
- Memory Management - objects usually reside in the Heap, you need to know how memory is managed.
- Static and Dynamic Typing - how and when the language checks what an object is capable of.
- Interfaces and Protocols - inheritance is used to created groups of related abstractions, with interfaces you can create groups of unrelated abstracts that all share responsibilities that must be accessed in a common way.
The following articles relate to implementing object oriented programs.
- Objects and Classes - which describes some of the main features of object oriented languages.
- Drawing Example - for a quick walk through that develops a drawing program and focuses on object interactions.