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 model for writing computer programs. Before OOP most programs were a list of instructions that acted on memory in the computer. Instead of a procedural list of actions, OOP 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 OOP principles for code reuse.
Some people who do not agree that OOP is easier/ better
Although OOP has gained in popularity, it has been met with criticism.
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] has shown no significant difference in productivity between OOP and procedural approaches.
- Christopher J. Date 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.[3]
- Alexander Stepanov suggested that OOP provides a mathematically-limited viewpoint and called it, "almost as much of a hoax as Artificial Intelligence" [4][5]
- Paul Graham, a successful web entrepreneur and programming author, has suggested that the purpose of OOP is to act as a herding mechanism which keeps mediocre programmers in mediocre organizations from "doing too much damage". This is at the expense of slowing down productive programmers who know how to use more powerful and more compact techniques. [1]
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 == 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 above code in Python.
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'
}
}
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