Object-oriented programming
From Wikipedia, the free encyclopedia
Object-oriented programming (OOP) is a programming paradigm that uses "objects" to design applications and computer programs. It utilizes several techniques from previously established paradigms, including inheritance, modularity, polymorphism, and encapsulation. Even though it originated in the 1960s, OOP was not commonly used in mainstream software application development until the 1990s. Today, many popular programming languages (such as ActionScript, Ada 95/2005, C#, C++, Delphi, Java, JavaScript, Lisp, Objective-C, Perl, PHP, Python, RealBasic, Ruby, Squeak, VB.Net, Visual FoxPro, and Visual Prolog) support OOP.
Object-oriented programming's roots reach all the way back to the 1960s, when the nascent field of software engineering had begun to discuss the idea of a software crisis. As hardware and software became increasingly complex, how could software quality be maintained? Object-oriented programming addresses this problem by strongly emphasizing modularity in software.[1]
The Simula programming language was the first to introduce the concepts underlying object-oriented programming (objects, classes, subclasses, virtual methods, coroutines, garbage collection and discrete event simulation) as a superset of Algol. Smalltalk was the first programming language to be called "object-oriented".
Object-oriented programming may be seen as a collection of cooperating objects, as opposed to a traditional view in which a program may be seen as a list of instructions to the computer. In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects. Each object can be viewed as an independent little machine with a distinct role or responsibility.[2]
Object-oriented programming came into existence because human consciousness, understanding and logic are highly object-oriented. By way of "objectifying" software modules, it is intended to promote greater flexibility and maintainability in programming, and is widely popular in large-scale software engineering. By virtue of its strong emphasis on modularity, object oriented code is intended to be simpler to develop and easier to understand later on, lending itself to more direct analysis, coding, and understanding of complex situations and procedures than less modular programming methods.[citation needed]
Contents |
[edit] Fundamental concepts of Object Oriented Programming
A survey by Deborah J. Armstrong[3], of nearly 40 years of computing literature, identified a number of "quarks," or fundamental concepts, identified in the strong majority of definitions of OOP. They are:
- Class
- A class defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes or properties) and the things it can do (its behaviors or methods or features). For example, the class
Dog
would consist of traits shared by all dogs, for example breed, fur color, and the ability to bark. Classes provide modularity and structure in an object-oriented computer program. A class should typically be recognizable to a non-programmer familiar with the problem domain, meaning that the characteristics of the class should make sense in context. Also, the code for a class should be relatively self-contained. Collectively, the properties and methods defined by a class are called members. - Object
- A particular instance of a class. The class of
Dog
defines all possible dogs by listing the characteristics that they can have; the objectLassie
is one particular dog, with particular versions of the characteristics. ADog
has fur;Lassie
has brown-and-white fur. In programmer jargon, the objectLassie
is an instance of theDog
class. The set of values of the attributes of a particular object is called its state. - Method
- An object's abilities.
Lassie
, being aDog
, has the ability to bark. Sobark()
is one ofLassie
's methods. She may have other methods as well, for examplesit()
oreat()
. Within the program, using a method should only affect one particular object; allDog
s can bark, but you need one particular dog to do the barking. - Message passing
- "The process by which an object sends data to another object or asks the other object to invoke a method."[3]
- Inheritance
- In some cases, a class will have "subclasses," more specialized versions of a class. For example, the class
Dog
might have sub-classes calledCollie
,Chihuahua
, andGoldenRetriever
. In this case,Lassie
would be an instance of theCollie
subclass. Subclasses inherit attributes and behaviors from their parent classes, and can introduce their own. Suppose theDog
class defines a method calledbark()
and a property calledfurColor
. Each of its sub-classes (Collie
,Chihuahua
, andGoldenRetriever
) will inherit these members, meaning that the programmer only needs to write the code for them once. Each subclass can alter its inherited traits. So, for example, theCollie
class might specify that the defaultfurColor
for a collie is brown-and-white. TheChihuahua
subclass might specify that thebark()
method is high-pitched by default. Subclasses can also add new members. TheChihuahua
subclass could add a method calledtremble()
. So an individual chihuahua instance would use a high-pitchedbark()
from theChihuahua
subclass, which in turn inherited the usualbark()
fromDog
. The chihuahua object would also have thetremble()
method, butLassie
would not, because she is aCollie
, not aChihuahua
. In fact, inheritance is an "is-a" relationship:Lassie
is aCollie
. ACollie
is aDog
. Thus,Lassie
inherits the members of bothCollie
s andDog
s. When an object or class inherits its traits from more than one ancestor class, and neither of these ancestors is an ancestor of the other, then it's called multiple inheritance. For example, independent classes could defineDog
s andCat
s, and aChimera
object could be created from these two which inherits all the (multiple) behaviour of cats and dogs. This is not always supported, as it can be hard both to implement and to use well. - Encapsulation
- Conceals the exact details of how a particular class works from objects that use its code or send messages to it. So, for example, the
Dog
class has abark()
method. The code for thebark()
method defines exactly how a bark happens (e.g., byinhale()
and thenexhale()
, at a particular pitch and volume). Timmy,Lassie
's friend, however, does not need to know exactly how she barks. Encapsulation is achieved by specifying which classes may use the members of an object. The result is that each object exposes to any class a certain interface — those members accessible to that class. The reason for encapsulation is to prevent clients of an interface from depending on those parts of the implementation that are likely to change in future, thereby allowing those changes to be made more easily, that is, without changes to clients. For example, an interface can ensure that puppies can only be added to an object of the classDog
by code in that class. Members are often specified as public, protected or private, determining whether they are available to all classes, sub-classes or only the defining class. Some languages go further: Java uses the protected keyword to restrict access also to classes in the same package, C# and VB.NET reserve some members to classes in the same assembly using keywords internal (C#) or Friend (VB.NET), and Eiffel and C++ allows one to specify which classes may access any member. - Abstraction
- Simplifying complex reality by modeling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem. For example,
Lassie
theDog
may be treated as aDog
much of the time, aCollie
when necessary to accessCollie
-specific attributes or behaviors, and as anAnimal
(perhaps the parent class ofDog
) when counting Timmy's pets.
- Polymorphism
- Polymorphism is the ability of behavior to vary based on the conditions in which the behavior is invoked, that is, two or more methods, as well as operators (such as +, -, *, among others) can fit to many different conditions. For example, if a
Dog
is commanded tospeak()
this may elicit aBark
; if aPig
is commanded tospeak()
this may elicit anOink
. This is expected becausePig
has a particular implementation inside thespeak()
method. The same happens to classDog
. Considering both of them inheritspeak()
fromAnimal()
, this is an example of Overriding Polymorphism. Another good example is about Overloading Polymorphism, a very common one considering operators, like "+". Once defined an operator used to add numbers, given a classNumber
and also given two other classes that inherits fromNumber
, such asInteger
andDouble
. Any programmer expects to add two instances ofDouble
or two instances ofInteger
in just the same way, and more than this: Any programmer expects the same behavior to anyNumber
. In this case, the programmer must overload the concatenation operator, "+", by making it able to operate with bothDouble
andInteger
instances. The way it is done varies a little bit from one language to another and must be studied in more details according to the programmer interest. Most of the OOP languages support small differences in method signatures as polymorphism. it's very useful, once it improves code readability, to enable implicit conversions to the correct handling method when applyadd()
method to integers, like inadd(1,2)
, or to strings like inadd("foo","bar")
since the definitions of these signatures are available. In many OOP languages, such method signatures would be, respectively, very similar toadd(int a, int b)
andadd(String a, String b)
. This is an example of Parametric Polymorphism. The returned type and used modifiers, of course, depend on the programmer interests and intentions.
[edit] Prototype-based programming
Not all of the above concepts are to be found in all object-oriented programming languages. In particular, prototype-based programming does not typically use classes. As a result, a significantly different yet analogous terminology is used to define the concepts of object and instance, although there is no objects in these languages.
[edit] History
The concept of objects and instances in computing had its first major breakthrough with the PDP-1 system at MIT which was probably the earliest example of capability based architecture. Another early example was Sketchpad made by Ivan Sutherland in 1963; however, this was an application and not a programming paradigm. Objects as programming entities were introduced in the 1960s in Simula 67, a programming language designed for making simulations, created by Ole-Johan Dahl and Kristen Nygaard of the Norwegian Computing Center in Oslo. (Reportedly, the story is that they were working on ship simulations, and were confounded by the combinatorial explosion of how the different attributes from different ships could affect one another. The idea occurred to group the different types of ships into different classes of objects, each class of objects being responsible for defining its own data and behavior.) Such an approach was a simple extrapolation of concepts earlier used in analog programming. On analog computers, such direct mapping from real-world phenomena/objects to analog phenomena/objects (and conversely), was (and is) called 'simulation'. Simula not only introduced the notion of classes, but also of instances of classes, which is probably the first explicit use of those notions.
The Smalltalk language, which was developed at Xerox PARC in the 1970s, introduced the term Object-oriented programming to represent the pervasive use of objects and messages as the basis for computation. Smalltalk creators were influenced by the ideas introduced in Simula 67, but Smalltalk was designed to be a fully dynamic system in which classes could be created and modified dynamically rather than simply using static ones as in Simula 67. The ideas in Simula 67 were also used in many other languages, from derivatives of Lisp to Pascal.
Object-oriented programming developed as the dominant programming methodology[citation needed] during the mid-1980s,[citation needed] largely due to the influence of C++. Its dominance was further cemented by the rising popularity of graphical user interfaces, for which object-oriented programming is well-suited. An example of a closely related dynamic GUI library and OOP language can be found in the Cocoa frameworks on Mac OS X, written in Objective C, an object-oriented, dynamic messaging extension to C based on Smalltalk. OOP toolkits also enhanced the popularity of "event-driven programming" (although this concept is not limited to OOP). Some feel that association with GUIs (real or perceived) was what propelled OOP into the programming mainstream.
OOP also became increasingly popular for developing computer games during the 1990s.[citation needed] As the complexity of games grew, as faster hardware became more widely available and compilers (especially C++) matured, more and more games and their engines were written in OOP languages. Prominent C++ examples[4] include Starcraft, Diablo, and Warcraft III. Since almost all video games feature virtual environments which contain many, often thousands of objects that interact with each other in complex ways, OOP languages are particularly suited for game development.
At ETH Zürich, Niklaus Wirth and his colleagues had also been investigating such topics as data abstraction and modular programming. Modula-2 included both, and their succeeding design, Oberon, included a distinctive approach to object orientation, classes, and such. The approach is unlike Smalltalk, and very unlike C++.
Object-oriented features have been added to many existing languages during that time, including Ada, BASIC, Lisp, Fortran, Pascal, and others. Adding these features to languages that were not initially designed for them often led to problems with compatibility and maintainability of code.
In the past decade Java has emerged in wide use partially because of its similarity to C and to C++, but perhaps more importantly because of its implementation using a virtual machine that is intended to run code unchanged on many different platforms. This last feature has made it very attractive to larger development shops with heterogeneous environments. Microsoft's .NET initiative has a similar objective and includes/supports several new languages, or variants of older ones.
More recently, a number of languages have emerged that are primarily object-oriented yet compatible with procedural methodology, such as Python and Ruby. Besides Java, probably the most commercially important recent object-oriented languages are Visual Basic .NET and C# designed for Microsoft's .NET platform.
Just as procedural programming led to refinements of techniques such as structured programming, modern object-oriented software design methods include refinements such as the use of design patterns, design by contract, and modeling languages (such as UML).
[edit] OOP in scripting
In recent years, object-oriented programming has become especially popular in scripting programming languages. Python and Ruby are scripting languages built on OOP principles, while Perl and PHP have been adding object oriented features since Perl 5 and PHP 4.
The Document Object Model of HTML, XHTML, and XML documents on the Internet have bindings to the popular JavaScript/ECMAScript language. JavaScript is perhaps the best known prototype-based programming language.
[edit] Problems and patterns
There are a number of programming challenges which a developer encounters regularly in object-oriented design. There are also widely accepted solutions to these problems. The best known are the design patterns codified by Gamma et al, but in a more general sense the term "design patterns" can be used to refer to any general, repeatable solution to a commonly occurring problem in software design. Some of these commonly occurring problems have implications and solutions particular to object-oriented development.
[edit] Gang of Four design patterns
Design Patterns: Elements Reusable Object-Oriented Software is an influential book published in 1995 by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, sometimes casually called the "Gang of Four." Along with exploring the capabilities and pitfalls of object-oriented programming, it describes 23 common programming problems and patterns for solving them.
As of April 2005, the book was in its 32nd printing. While it can make for dense reading, even for experienced programmers, and has been superseded in practice by a spate of more recent, more accessible books, it is regarded as an important source not only for design patterns but also for the object-oriented design guidelines in its initial chapter.
[edit] Object-orientation and databases
Both object-oriented programming and relational database management systems (RDBMSs) are extremely common in software today. Since relational databases don't store objects directly (though some RDBMSs have object-oriented features to approximate this), there is a general need to bridge the two worlds. There are a number of widely used solutions to this problem. One of the most common is object-relational mapping, as found in libraries like Java Data Objects, and Ruby on Rails' ActiveRecord.
There are also object databases which can be used to replace RDBMSs, but these have not been as commercially successful as RDBMSs.
[edit] Matching real world
OOP can be used to translate from real-world phenomena to program elements (and vice versa). OOP was even invented for the purpose of physical modelling in the Simula-67 programming language. However, not everyone agrees that direct real-world mapping is facilitated by OOP, or is even a worthy goal; Bertrand Meyer argues in Object-Oriented Software Construction[5] that a program is not a model of the world but a model of a model of some part of the world; "Reality is a cousin twice removed".
[edit] Formal definition
There have been several attempts at formalizing the concepts used in object-oriented programming. The following concepts and constructs have been used as interpretations of OOP concepts:
|
Attempts to find a consensus definition or theory behind objects have not proven very successful, and often diverge widely. For example, some definitions focus on mental activities, and some on mere program structuring. One of the simpler definitions is that OOP is the act of using "map" data structures or arrays that can contain functions and pointers to other maps, all with some syntactic and scoping sugar on top. Inheritance can be performed by cloning the maps (sometimes called "prototyping").
[edit] Criticism
- One study by Potok et al. [1] has showed no significant difference in productivity between OOP and procedural approaches.
- Christopher J. Date has stated that critical comparison of OOP to other technologies, relational in particular, is difficult because of lack of an agreed-upon and rigorous definition of OOP.[6]
- Alexander Stepanov has suggested that OOP provides a mathematically-limited viewpoint and called it, "almost as much of a hoax as Artificial Intelligence" (possibly referring to the Artificial Intelligence projects and marketing of the 1980s that are sometimes viewed as overzealous in retrospect) [2].
[edit] See also
- Object-oriented programming language
- Aspect-oriented programming
- Procedural programming
- Object-oriented analysis and design
- Circle-ellipse problem
- Software componentry
- Interface description language
- List of object-oriented programming terms
- Refactoring
- CORBA
- DCOM
- Object-relational mapping
- Object-Relational impedance mismatch
- Object database
- Object-oriented image classification
[edit] Further reading
- Abadi, Martin; Luca Cardelli. A Theory of Objects. Springer-Verlag. ISBN 0-387-94775-2.
- Abelson, Harold; Gerald Jay Sussman,. Structure and Interpretation of Computer Programs. The MIT Press. ISBN 0-262-01153-0.
- Booch, Grady. Object-Oriented Analysis and Design with Applications. Addison-Wesley. ISBN 0-8053-5340-2.
- Eeles, Peter; Oliver Sims. Building Business Objects. John Wiley & Sons. ISBN 0-471-19176-0.
- Gamma, Erich; Richard Helm, Ralph Johnson, John Vlissides. Design Patterns: Elements of Reusable Object Oriented Software. Addison-Wesley. ISBN 0-201-63361-2.
- Harmon, Paul; William Morrissey. The Object Technology Casebook - Lessons from Award-Winning Business Applications. John Wiley & Sons. ISBN 0-471-14717-6.
- Jacobson, Ivar. Object-Oriented Software Engineering: A Use Case-Driven Approach. Addison-Wesley. ISBN 0-201-54435-0.
- Kay, Alan. The Early History of Smalltalk.
- Rumbaugh, James; Michael Blaha, William Premerlani, Frederick Eddy, William Lorensen. Object-Oriented Modeling and Design. Prentice Hall. ISBN 0-13-629841-9.
- Taylor, David A.. Object-Oriented Information Systems - Planning and Implementation. John Wiley & Sons. ISBN 0-471-54364-0.
- "OOP Better in Theory than in Practice" by Richard Mansfield
[edit] Notes
- ^ Meyer, chapter 3
- ^ Booch, chapter 2
- ^ a b Armstrong, "The Quarks of Object-Oriented Development." In descending order of popularity, the "quarks" are: Inheritance, Object, Class, Encapsulation, Method, Message Passing, Polymorphism, Abstraction
- ^ C++ Applications, by Bjarne Stroustrup, the author of C++
- ^ Meyer, Second Edition, p. 230
- ^ C. J. Date, Introduction to Database Systems, 6th-ed., Page 650
[edit] References
- Armstrong, Deborah J. (February 2006). "The Quarks of Object-Oriented Development". Communications of the ACM 49 (2): 123–128. ISSN 0001-0782. Retrieved on 2006-08-08.
- Meyer, Bertrand (1997). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629155-4.