Object-oriented programming
![]() | The English used in this article or section may not be easy for everybody to understand. (April 2013) |
Object-oriented programming, also called OOP, is a type of programming language model for writing computer programs. Before object-oriented programming, most programs were a list of instructions that acted on memory in the computer. Instead of a procedural list of actions, object-oriented programming is modeled around objects that interact with each other. Classes generate objects and define their structure, like a blueprint. The objects interact with each other to carry out the intent of the computer program.
Many design patterns have been written utilizing object-oriented programming principles for code reuse.
Example Code
This computer code is in the Python programming language.
class Human(object):
def __init__(self, name, friend=None):
self.name = name
self.friend = friend
def say_name(self):
print("My name is "+self.name)
def say_goodnight(self):
if self.friend is None:
print("Good night nobody.")
else:
print("Good night "+self.friend.name)
#create a new human object named stephen
stephen = Human("Stephen")
#create a human object named joe with stephen as a friend
joe = Human("Joe", stephen)
stephen.say_name() #shows 'My name is Stephen'
stephen.say_goodnight() #shows 'Good night nobody.'
joe.say_name() # shows 'My name is Joe'
joe.say_goodnight() #shows 'Good night Stephen'
This code is in Java. It does the same thing as the Python code above.
public class Human
{
private String name = "unnamed"; // the name of this human
private Human friend = null; // the human's friend
/**
* This "creates" a new Human
*/
public Human(String name, Human friend) {
this.name = name;
this.friend = friend;
}
public Human(String name) {
this.name = name;
this.friend = null;
}
public Human() {
this.name = "unnamed";
this.friend = null;
}
public void sayName() {
System.out.println("My name is " + this.name);
}
public void sayGoodnight() {
if (friend == null)
System.out.println("Good night nobody.");
else
System.out.println("Good night " + friend.name);
}
}
public class Main
{
public static void main(String[] args) {
//create a new human object named stephen
Human stephen = new Human("Stephen");
//create a human object named joe with stephen as a friend
Human joe = new Human("Joe", stephen);
stephen.sayName(); //shows 'My name is Stephen'
stephen.sayGoodnight(); //shows 'Good night nobody.'
joe.sayName(); // shows 'My name is Joe'
joe.sayGoodnight(); //shows 'Good night Stephen'
}
}
This code is in C++. It does the same thing as the Java code above.
class Human {
private:
string name; // The name of this human
Human *mFriend; // The human's friend
public:
// This "creates" a new Human
Human(string Name, Human Friend) {
name = Name;
mFriend = &Friend;
}
Human(string Name) {
name = Name;
mFriend = NULL;
}
Human() {
name = "unnamed";
mFriend = NULL;
}
void sayName() {
cout << "My name is " << name << endl;
}
void sayGoodnight() {
if (!mFriend)
cout << "Good night nobody" << endl;
else {
cout << "Good night " << mFriend->name << endl;
}
}
};
int main() {
Human stephen = Human("Stephen"); //create a new human object named stephan
Human joe = Human("Joe", stephen); //create a human object named joe with stephen as a friend
stephen.sayName(); //shows 'My name is Stephen'
stephen.sayGoodnight(); //shows 'Good night nobody.'
joe.sayName(); //shows 'My name is Joe'
joe.sayGoodnight(); //shows 'Good night Stephen'
return 0;
}
Criticism of Object Oriented Programming
OOP has become popular, but many people criticize it.
Luca Cardelli wrote a paper titled 'Bad Engineering Properties of Object-Oriented Languages'.
- Richard Stallman wrote in 1995, "Adding OOP to Emacs is not clearly an improvement; I used OOP when working on the Lisp Machine window systems, and I disagree with the usual view that it is a superior way to program."[1]
- A study by Potok et al.[2] tells us that there is very little difference in productivity between OOP and procedural approaches.
- Christopher J. Date said that comparing OOP to other things, especially how OOP and the other thing are related, is difficult because people don't agree on the meaning of OOP.[3]
- Alexander Stepanov suggested that OOP gives a point of view that is limited as far as math, and called it, "almost as much of a hoax as Artificial Intelligence" [4][5]
- Paul Graham, a successful internet salesman and programmer, has suggested that the purpose of OOP is to act as a herding mechanism which keeps average programmers in average organizations from "doing too much damage". This also slows down faster, better programmers who know how to do things in a more powerful and more compact way.. [1]
References
- ↑ "Mode inheritance, cloning, hooks & OOP (Google Groups Discussion)".
- ↑ http://www.csm.ornl.gov/~v8q/Homepage/Papers%20Old/spetep-%20printable.pdf
- ↑ C. J. Date, Introduction to Database Systems, 6th-ed., Page 650
- ↑ The AI Effect
- ↑ STLport: An Interview with A. Stepanov