Object-oriented programming

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

Object-oriented programming or OOP has become an important subject for computers. In the past, programs were written mostly as 'a list of instructions that did things' to the memory in the computer (for instance, they added numbers together, moved words about and saved or printed the answers). Another way that this can be thought of is that the numbers and words are in fact simple 'objects' that can be changed using different 'methods'. That is what gives the name to this different method. These objects are independent and can only 'talk' to other objects they are allowed to by the rules (which are decided by the programmer). It is claimed that this makes it easier to program because the programmer only needs to worry about building one component (object) at a time that they say is easier than having to think about the whole program at once.

An object can receive messages, process information, and 'talk' to other objects. Each object can be thought of as a part of the whole.

It is also claimed that object-oriented programming can mean programs are easier to maintain, because each object can be dealt with by itself. It is very popular in software engineering, (even though many people do not agree that it is better than earlier methods and actually produces entirely new problems of its own making that it then has to solve).

[change] Some people who do not agree that OOP is easier/ better

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]

[change] Example Code

This computer code is in 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'
    }
}

[change] References

Personal tools
Namespaces

Variants
Actions
Getting around
Print/export
Toolbox
In other languages