trialsite 0 Report post Posted May 1, 2008 Object Orientated Programming What is Object Orientated Programming? Object Orientated Programming, or OOP as it is also commonly called, is a way of programming which essentially mimics how objects work in the physical world. Unlike procedural programming, which works essentially from a list of instructions carried out one after the other, object orientated programming used objects and classes, which not only save time and space, but also makes programming easier. The way it works it that rather than breaking down the problem into a series of steps to be followed, the program instead is a set of objects, which work together to perform the task in hand. It would help to first understand what an object or class is before we go any further. What is an Object or Class? Consider the example of a football team. The team is full of football players; however that is not to say that they are all the same, and share the same values. For example, they all play football, but they have different names, positions, etc? and for that reason they are different. In this instance the class that they belong to is the Footballer class, as they are all footballers. However they are all individuals, and in this instance we would refer to them as objects. However, this is not to say that an object needs to be a physical object. For example it could be a bank account, or a web hosting account. Class A class is a template from which an object is created. Every object that is created from it will have the same type of attribute, but their values may be different. Object An object is an instance of a class. Or in other words, it is an object created using the template of the class it is derived from. Fortunately using the class system you are able to reuse the class to create many objects that share similar characteristics to the class, but which have very different values. Using the football player as an example you can create many different player objects, all of which are footballers, but have different values, such as different names, positions, etc? The way the program works is that the class is created before the program loads, and then whilst the program is running, the objects are created. This means that the objects can be created as and when they need to be, which consequently greatly increases the efficiency of the program as an object is only created when it is needed. Attributes and Behaviour An object has certain properties such as (in the case of a button) colour, height, size, etc? but it also acts in a certain way once called upon (or clicked), and it is this which makes the idea of an object important. The fact that these values can be changed or altered is what allows the class function to be used so efficiently. Class Attributes Using the example of the Footballer object from before, we have already established that each footballer is different properties such as its position and name. We refer to these properties as the class? attribute. Attributes Attributes are the properties that distinguish one object from another. The values given to the attributes are referred to as variables. In the example of the Footballer, the ?position? field would be the attribute and the variable would be ?defender, midfielder or striker?. These variables are not fixed, and can change throughout the program if required. A value that does not or can not change is called a constant. Instance Variable An instance variable (also called an object variable) defines the value of an attribute of an object (or instance). Using the example of the football team, this would refer to the individual player, i.e. their name, position, etc? Class Variable A class variable defines the value of an attribute of a class. Using the example of the football team, as this would apply to the entire class it could mean different things. If all of the footballers belonged to the same team, the class could have an attribute called ?Team Name?, which would need to be the same for all players. By changing the class field it saves much more time as it will automatically apply to all of the footballer objects that have been created. For example, if there are 15 Footballer objects, if the name of the team changed, only the one class variable (which applies to all players) would need to be changed. Otherwise 15 different objects would have to be changed, and this could take a long time. Class Behaviour As with all objects (both in Java, and in the physical world) the reason they exist is because they serve a purpose; a washing machine washes clothes, a light turns on and off, etc? The actions that a class or object performs are referred to as their behaviours. Behaviour The behaviour of an object or class refers to the actions that they can perform. Like kicking a football, all actions need to be described before they can be performed. In other words, a football player cannot kick a ball without first knowing that he must place the supporting foot beside the ball, and swing the striking foot until contact is made. These ?instructions? that explain how a particular action is performed is called a method. Method A method defines how a particular task is to be completed. Objects also communicate with each other using method, whether it is providing each other with confirmation of their current location, or to inform them of a change in situation (in the case of a footballer object). As with variables, you can get instance methods and class methods. As their names suggest, an instance method relates to a method for a particular object, and a class method relates to a method for a class itself. Parameter Methods may have parameters to pass additional information needed to do the action. Inheritance Inheritance is one of the most important ideas in object-orientated programming, and it greatly affects the way in which classes are written. As a result of inheritance a class instantly adopts all of the functionality of the superclass it has inherited from. Inheritance Inheritance is where one class inherits the behaviour and attributes of another. A class that inherits from another is called a subclass and a class that provides the inheritance is called a superclass. This naturally creates a hierarchy system amongst classes (this will be explained later), and it is important in establishing the structure of the program. In practice this means that in order to create a new class you don?t have to define each of its characteristics from scratch, and instead you can simply inherit the attributes from a similar pre-existing class and just amend the attributes that differ. Class Hierarchy As previously mentioned, this creates a hierarchy whereby classes inherit characteristics of others, and consequently superclasses and subclasses are formed. The process could be compared to when a baby inherits features from their parents through their genes. The child has an original set of characteristics (in base ?egg form?) and it is then affected by other factors to change is slightly so that is different. However, no matter how different it has become, it still has ultimately inherited behaviours and attributes from their parents (this could be things like eye colour, height, etc?) Taking this analogy further you could trace it back to the Garden of Eden or the big bang, etc? It is important to note however (unlike human genes), classes can only inherit features from one other, i.e. it cannot ?mix and match? components from two different classes. However, a superclass may have many subclasses. A class only ever has one superclass, but it can have many subclasses. Using this approach it is a lot faster to create new classes, as (like creating objects) there is a template to start with. Subclassing Subclassing is the method by which a new class is created by inheriting features from a superclass. Methods Methods define objects? behaviour ? anything that happens when the object is created and the various tasks that it performs throughout its lifetime. Method Methods implement the behaviour of objects. The structure of a method consists of a header and a body. The header defines the methods signature: public int getPrice () Not all methods return a result. Some do and some don?t. This is reflected in the signature of the method. All methods with ?void? in their signature do not return a result. There are two types of method. Accessor Methods Accessor methods provide information about an object ? they ?return? it. public int getPrice () { return price; } Accessor method A method which returns data. Mutator Methods A Mutator method (as the name suggests) is used to change the state of the object. This is achieved by changing the value of one or more fields. Typically contain assignment statements. Typically receive values as parameters. public void insertMoney (int amount) { Balance += amount; } In the above example, it is important to point out that ?balance += amount? is the same as writing ?balance = balance + amount?. Mutator method A method which changes an objects state. Share this post Link to post Share on other sites
dimumurray 0 Report post Posted July 30, 2008 You've touched on some of the basic principles of OOP, but you need to round out your article with a discussion of encapsulation and polymorphism. If you plan on making this a series of articles on Object Oriented Programming, you will want to look at discussing Design Patterns and Frameworks when you start on the more advanced topics. All in all not a bad job. P.S. Consider supplying a list of must-read books on OOP (Anything from the Head-First Series gets my vote ) Share this post Link to post Share on other sites
longer 0 Report post Posted August 5, 2008 You've touched on some of the basic principles of OOP, but you need to round out your article with a discussion of encapsulation and polymorphism. If you plan on making this a series of articles on Object Oriented Programming, you will want to look at discussing Design Patterns and Frameworks when you start on the more advanced topics. All in all not a bad job. P.S. Consider supplying a list of must-read books on OOP (Anything from the Head-First Series gets my vote ) Agreed. They're only some of the very basic concepts. Also something like multithreading could be a topic as well. It's not possible to include all aspects in one article though. Share this post Link to post Share on other sites
dimumurray 0 Report post Posted August 12, 2008 (edited) It's not possible to include all aspects in one article though. True...Hence the reason I suggested a series of articles...Come to think of it maybe we should start doing just that...We would have to finish up the basics first though. We could map out a list of topics, creating a pool of sorts. Then have people interested in writing up an article pick the ones they feel most proficient in and have them write something on it (after doing some extensive research first) Here's a list of topics in addition to those already discussed by trialsite, somewhat categorized: BasicsEncapsulation, Data-hidingAbstractionPolymorphismComposition Design PatternsSingletonStrategyFactoryDecoratorObserverIterator & CompositeStateFacadeAdapterCommandProxyMVC-Model View Controller FrameworksGranted this is by no means an exhaustive list, so feel free to add to it...That is assuming that anyone is interested in the idea... PS(longer): I'm not exactly sure but I don't think multi-threading falls under the auspice of OOP (to me it's more of a language feature) even though the two often go hand in hand. That's why I didn't list it . Still I may be wrong...if so feel free to expound on the issue... Edited August 12, 2008 by dimumurray (see edit history) Share this post Link to post Share on other sites
xpress 0 Report post Posted August 16, 2008 Are these generic OOPS concepts or specific to some language? Share this post Link to post Share on other sites
moutonoir 0 Report post Posted August 16, 2008 The concepts are pretty generic but some terms, such as "method," are specific to certain languages. There are methods in Java, for example, but in C++ they're called functions. Also the examples given are in Java; syntax varies slightly among different languages. Still though, if you wanted an understanding about object orientation in general, these concepts certainly apply. Share this post Link to post Share on other sites