You are on page 1of 4

What is Object-Oriented Programming ? OO programming is designed so that real world concepts can be modeled in a computer program.

This concept is rather difficult to explain so I'll use an example throughout this primer. The example I am going to use is that of a bicycle. Object-oriented programming thinks of real world things as objects, so in the case of the example the bicycle is an object. Objects have two parts to them, data and operations which can be carried out on this data. So in the bicycle example, the data might contain the speed of the bike, the height of the seat and whether the bell is currently being rung or not. There are several different operations which can be carried out on the bicycle, the rider may pedal faster, they might want to change the height of the seat of ring the bell. So in this simple object we have the following: Data: Speed Height of seat Status of the bell Operations: Pedal Faster Adjust Height of Seat Ring Bell Encapsulation Encapsulation is an important part of OO programming, but it's not difficult. In Java Encapsulation is implemented by a class, all a class is the generic form of an object. So in our example the class is bicycle while the object (an instance of the class) is, perhaps MyBicycle. Within the object there are so called instance variables, in our example MyBicycle will have instance variables for speed, height of seat and status of the bell. So when an operation (in Java a method) operates on an object it changes the instance variables for that object. If the Pedal Faster method was called for the object MyBicycle then the instance variable of speed, within MyBicycle, would be altered accordingly. An additional complexity is that methods and instance variables can be public or private. If a method is private it can only be accessed by code within the implementation of the class, while private instance variables can only be accessed from within the class. Public methods and instance variables can be accessed from any class.

If this were likened to an object. So for our example there will be two methods called collision. used correctly it can lead to the development of very robust. We use encapsulation all the time. Polymorphism Polymorphism is the ability of objects to react differently when presented with different information. Object-oriented languages. or to inherit characteristics. The bicycle in the example is a very simple one. Expanding this simple car to be a racing car with . the gear lever would be the method while the internal workings of the clutch and gearbox would be the instance variables. while the class they belong to is not always obvious. to create a sub-class. When a class is extended. Conclusion on Object-Oriented Programming Object-oriented programming is fundamentally different to traditional functional programming. one to change up a gear and one to change down. if we wanted to extend it to have gears we would simply create a new class based on Bicycle but which had a new variable called gear and two new methods. a number of methods are written (with the same name) but each one has a different set of input parameters. So if our bicycle were to be involved in a collision with a object the object would be passed as a parameter to a collision method. When a class is extended a hierarchy is built up with the original class at the top and the classes which build on the class above it (the class at the top is known as the super-class) below. obviously collision with a fly will have very different affects to collision with a lorry. easily expandable and maintainable code. In a functional programming language the only way to complete two different tasks is to have two functions with different names. This ability of a method to react to different parameters is achieved by overriding. all of the properties (variables and methods) of the original class still exist within the new class along with others which have been added. when we change gear in a car we do not need to know what happens with the clutch or gearbox we simply need to know that moving the gear lever will change gear. The fly may cause the bicycle to gain a speck of blood while collision with a lorry may cause a sudden loss of speed and major damage. allow different methods to be run depending on what type of parameters are specified.Inheritance Inhertiance is a relatively simple concept which allows one class to extend another. known as parameters. Objects are everywhere around us. which include Java. one which accepts a parameter of type fly while the other accepts a parameter of type lorry.

"reportIncome" and "increaseCommisionRate".wings and spoilers is an example of inheritance while the car's ability to cope with steering movements to the left and right is an example of polymorphism. and many more.). its average temperature. in attempting to understand the mechanics of the solar system. or understanding can take place. getSick. experimentation. a credit record. value of vehicles sold. commission rate. The objects in an object-oriented system are often intended to correspond directly to entities in the "real world". etc. Trying to capture even a small part of this enormous detail in a software object is pointless. its atmospheric content. For example. Abstraction is concerned with both the "attributes" and "behavior". Info about Abstraction: Abstraction is a design technique that focuses on the essential aspects of an entity and ignores or conceals less important or non-essential aspects. these other details are not relevant to understanding and modeling the basic orbital mechanics of the solar system. An action usually corresponds to an action that the real-world entity might perform. The "behavior" represents the set of actions that the object can perform. For a sales tracking system relevant attributes of a salesperson might be: name. a medical history. haveChild. a set of talents. What is important is to capture those aspects of a "salesperson" that are relevant to the development of the sales tracking system. increaseCreditLimit. Consider the simple "salesperson" object referred to above. Actions for a "salesperson" might include "sellCar". a family genealogy. Attributes typically correspond to the data that is recorded for an object.its actual diameter. Similarly there is a rich set of actions of which the salesperson is capable (singSong. Thus. A real "salesperson" has an identity. treating the planet as a body all of whose mass is concentrated at a single point. total commissions. a genetic profile. abstraction ignores a wealth of details about each planet . Abstraction is an important tool for simplifying a complex situation to a level where analysis. payBills. Attributes refer to the properties or characteristics associated with an entity. doDance. number of vehicles sold. etc. Abstraction is vital to creating tractable software objects because the real-world objects are far too complex to be captured in complete detail. However. The correspondence between the software objects and the real-world entity that they represent is often so direct and real that . early mathematicians and astronomers applied abstraction to a "planet". list of customers. Objects such "salesperson" and "automobiles" that might occur in an automobile dealership tracking system correspond to the actual people on the staff of the dealership and the actual cars owned and sold by the dealership.

changes in one being reflected in the other. This sense of correspondence is also expressed as the "program" being a "simulation" or " model" of the real-world. . A "good" program is one which models or simulates accurately what is happening in the theft or fraud often involves tampering with the software objects that are trusted by others to correspond to real-world artifacts.