Object-oriented programming

From Wikipedia, the free encyclopedia
Jump to: navigation, search

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.

Criticism of Object Oriented Programming[change | edit source]

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 o 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]

Example Code[change | edit source]

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 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[change | edit source]