Jump to content

Object-oriented programming: Difference between revisions

From Simple English Wikipedia, the free encyclopedia
Content deleted Content added
Qwertyxp2000 (talk | changes)
No edit summary
Attempt to simplify and expand article
Line 1: Line 1:
'''Object-oriented programming''' ('''OOP''') is a way of writing computer programs using the idea of "[[Object (computer science)|objects]]" to represent [[data]] and [[Method (computer science)|methods]]. Usually, computer programs were just a list of instructions to the computer, telling the computer to do certain things in a certain way, which is called [[procedural programming]]. However in object-oriented programming, computer programs make use of objects that talk to one another and to change the data in those objects, to work in a way that the user wants. Also, because of the way object-oriented programming is designed, it helps the developer by allowing for code to be easily reused by other parts of the program or even by other people.
{{unsimple|date=April 2013}}


Most [[Programming language]]s is a mix of different types of ways of writing computer programs. For example, [[Python (programming language)|Python]] allows for computer programs to be written both in object-oriented programming and in procedural programming. There are many programming languages that allows you to write computer programs in OOP, some of which are [[C++]], [[Java (programming language)|Java]], [[Ruby (programming language)|Ruby]], [[Perl]], [[PHP]], etc.
'''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.


== Features ==
Many design patterns have been written utilizing object-oriented programming principles for code reuse.
The main idea of object-oriented programming is that everything is an object. However, the object can be of different types:


* [[Variable]]s can hold information (or data) of different types that is supported by the programming language. Examples are the [[integer]] [[data type]] and the lists [[data structures|data structure]]. Variables is an idea that is already available from procedural programming languages. In object-oriented programming, variables are mainly known as attributes.
== Example Code ==
* Procedures (also known as functions, methods or routines) are a list of instructions telling the computer to take input, do some calculations or change data, and return output to the user. Procedures is also an idea that is already available from procedural programming languages. In object-oriented programming, procedures are mainly known as methods.
* Classes are a collection of different variables and procedures. Classes usually talk to one another to make changes to data in order to work in a way that the user wants.

Objects is a term used to refer to instances of classes.

== Examples ==
In the examples below, we create a class called <code>Human</code>. This class contains the attributes <code>name</code> (for the name of the person) and <code>friend</code> (for the name of the friend). Each of the methods in the <code>Human</code> class contains a list of instructions that makes use of both the <code>name</code> and <code>friend</code> attributes.

=== Python ===
This code is in [[Python (programming language)|Python]].


=== This computer code is in the [[Python (programming language)|Python programming language]]. ===
<source lang="python">
<source lang="python">
class Human(object):
class Human(object):
Line 21: Line 31:
print("Good night "+self.friend.name)
print("Good night "+self.friend.name)


#create a new human object named stephen
# Create a new Human object named stephen with name "Stephen"
stephen = Human("Stephen")
stephen = Human("Stephen")
#create a human object named joe with stephen as a friend
# Create a new Human object named joe with name "Joe" and stephen as a friend
joe = Human("Joe", stephen)
joe = Human("Joe", stephen)


stephen.say_name() #shows 'My name is Stephen'
stephen.say_name() # Shows 'My name is Stephen'
stephen.say_goodnight() #shows 'Good night nobody.'
stephen.say_goodnight() # Shows 'Good night nobody.'
joe.say_name() # shows 'My name is Joe'
joe.say_name() # Shows 'My name is Joe'
joe.say_goodnight() #shows 'Good night Stephen'
joe.say_goodnight() # Shows 'Good night Stephen'
</source>
</source>

This code is in [[Java (programming language)|Java]]. It does the same thing as the [[Python (programming language)|Python code]] above.
=== Java ===
This code is in [[Java (programming language)|Java]].

; The Human class
<source lang="java">
<source lang="java">
public class Human
class Human {
private String name = "unnamed"; // the name of this Human
{
private String name = "unnamed"; // the name of this human
private Human friend = null; // the Human's friend

private Human friend = null; // the human's friend
// This method creates a new Human object when given the name and friend
/**
* This "creates" a new Human
*/
public Human(String name, Human friend) {
public Human(String name, Human friend) {
this.name = name;
this.name = name;
this.friend = friend;
this.friend = friend;
}
}

// This method also creates a new Human object when only given the name
public Human(String name) {
public Human(String name) {
this.name = name;
this.name = name;
this.friend = null;
this.friend = null;
}
}

// This method creates a new Human object when not given both the name and the friend
public Human() {
public Human() {
this.name = "unnamed";
this.name = "unnamed";
this.friend = null;
this.friend = null;
}
}

public void sayName() {
public void sayName() {
System.out.println("My name is " + this.name);
System.out.println("My name is " + this.name);
}
}

public void sayGoodnight() {
public void sayGoodnight() {
if (friend == null)
if (friend == null) {
System.out.println("Good night nobody.");
System.out.println("Good night nobody.");
else
} else {
System.out.println("Good night " + friend.name);
System.out.println("Good night " + friend.name);
}
}
}
}
}
</source>
</source>

; A method for talking to the Human class above
<source lang="java">
<source lang="java">
public class Main
class Main {
{
public static void main(String[] args) {
public static void main(String[] args) {
//create a new human object named stephen
// Create a new Human object stephen with name "Stephen"
Human stephen = new Human("Stephen");
Human stephen = new Human("Stephen");

//create a human object named joe with stephen as a friend
// Create a new Human object joe with name "Joe" and stephen as a friend
Human joe = new Human("Joe", stephen);
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'
}
}
</source>This code is in C++. It does the same thing as the [[Java (programming language)|Java code]] above.<syntaxhighlight lang="c++">
class Human {
private:
string name; // The name of this human
Human *mFriend; // The human's friend
public:


stephen.sayName(); // Shows 'My name is Stephen'
// This "creates" a new Human
stephen.sayGoodnight(); // Shows 'Good night nobody.'
Human(string Name, Human Friend) {
name = Name;
joe.sayName(); // Shows 'My name is Joe'
joe.sayGoodnight(); // Shows 'Good night Stephen'
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;
}
}
};
</syntaxhighlight><syntaxhighlight lang="c++">
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;
}
}
</source>
</syntaxhighlight>


== Criticism ==
Even though object-oriented programming is popular, but there are many people who think that it is bad and criticize it.


* [[Luca Cardelli]] wrote a paper titled '[http://doc.cat-v.org/programming/bad_properties_of_OO Bad Engineering Properties of Object-Oriented Languages]'.
== Criticism of Object Oriented Programming ==
OOP has become popular, but many people criticize it.

[[Luca Cardelli]] wrote a paper titled '[http://doc.cat-v.org/programming/bad_properties_of_OO 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 system]]s, and I disagree with the usual view that it is a superior way to program."<ref>{{cite web|url=http://groups.google.com/group/comp.emacs.xemacs/browse_thread/thread/d0af257a2837640c/37f251537fafbb03?lnk=st&q=%22Richard+Stallman%22+oop&rnum=5&hl=en#37f251537fafbb03|title=Mode inheritance, cloning, hooks & OOP (Google Groups Discussion)}}</ref>
* [[Richard Stallman]] wrote in 1995, "Adding OOP to [[Emacs]] is not clearly an improvement; I used OOP when working on the [[Lisp Machine]] [[window system]]s, and I disagree with the usual view that it is a superior way to program."<ref>{{cite web|url=http://groups.google.com/group/comp.emacs.xemacs/browse_thread/thread/d0af257a2837640c/37f251537fafbb03?lnk=st&q=%22Richard+Stallman%22+oop&rnum=5&hl=en#37f251537fafbb03|title=Mode inheritance, cloning, hooks & OOP (Google Groups Discussion)}}</ref>
* A study by Potok et al.<ref>http://www.csm.ornl.gov/~v8q/Homepage/Papers%20Old/spetep-%20printable.pdf</ref> tells us that there is very little difference in productivity between OOP and procedural approaches.
* A study by Potok et al.<ref>http://www.csm.ornl.gov/~v8q/Homepage/Papers%20Old/spetep-%20printable.pdf</ref> tells us that there is very little difference in productivity between OOP and procedural approaches.
Line 138: Line 110:
* [[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" <ref>[http://www.aaai.org/aitopics/html/aieffect.html The AI Effect<!-- Bot generated title -->]</ref><ref>[http://www.stlport.org/resources/StepanovUSA.html STLport: An Interview with A. Stepanov<!-- Bot generated title -->]</ref>
* [[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" <ref>[http://www.aaai.org/aitopics/html/aieffect.html The AI Effect<!-- Bot generated title -->]</ref><ref>[http://www.stlport.org/resources/StepanovUSA.html STLport: An Interview with A. Stepanov<!-- Bot generated title -->]</ref>
* [[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.. [http://www.paulgraham.com/noop.html]
* [[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.. [http://www.paulgraham.com/noop.html]



== References ==
== References ==

Revision as of 16:16, 8 November 2018

Object-oriented programming (OOP) is a way of writing computer programs using the idea of "objects" to represent data and methods. Usually, computer programs were just a list of instructions to the computer, telling the computer to do certain things in a certain way, which is called procedural programming. However in object-oriented programming, computer programs make use of objects that talk to one another and to change the data in those objects, to work in a way that the user wants. Also, because of the way object-oriented programming is designed, it helps the developer by allowing for code to be easily reused by other parts of the program or even by other people.

Most Programming languages is a mix of different types of ways of writing computer programs. For example, Python allows for computer programs to be written both in object-oriented programming and in procedural programming. There are many programming languages that allows you to write computer programs in OOP, some of which are C++, Java, Ruby, Perl, PHP, etc.

Features

The main idea of object-oriented programming is that everything is an object. However, the object can be of different types:

  • Variables can hold information (or data) of different types that is supported by the programming language. Examples are the integer data type and the lists data structure. Variables is an idea that is already available from procedural programming languages. In object-oriented programming, variables are mainly known as attributes.
  • Procedures (also known as functions, methods or routines) are a list of instructions telling the computer to take input, do some calculations or change data, and return output to the user. Procedures is also an idea that is already available from procedural programming languages. In object-oriented programming, procedures are mainly known as methods.
  • Classes are a collection of different variables and procedures. Classes usually talk to one another to make changes to data in order to work in a way that the user wants.

Objects is a term used to refer to instances of classes.

Examples

In the examples below, we create a class called Human. This class contains the attributes name (for the name of the person) and friend (for the name of the friend). Each of the methods in the Human class contains a list of instructions that makes use of both the name and friend attributes.

Python

This code is in Python.

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 with name "Stephen"
stephen = Human("Stephen")
# Create a new Human object named joe with name "Joe" and 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'

Java

This code is in Java.

The Human class
class Human {
    private String name = "unnamed"; // the name of this Human
    private Human friend = null; // the Human's friend

    // This method creates a new Human object when given the name and friend
    public Human(String name, Human friend) {
        this.name = name;
        this.friend = friend;
    }

    // This method also creates a new Human object when only given the name
    public Human(String name) {
        this.name = name;
        this.friend = null;
    }

    // This method creates a new Human object when not given both the name and the friend
    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);
        }
    }
}
A method for talking to the Human class above
class Main {
    public static void main(String[] args) {
        // Create a new Human object stephen with name "Stephen"
        Human stephen = new Human("Stephen");

        // Create a new Human object joe with name "Joe" and 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'
    }
}

Criticism

Even though object-oriented programming is popular, but there are many people who think that it is bad and 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..

References

  1. ↑ "Mode inheritance, cloning, hooks & OOP (Google Groups Discussion)".
  2. ↑ http://www.csm.ornl.gov/~v8q/Homepage/Papers%20Old/spetep-%20printable.pdf
  3. ↑ C. J. Date, Introduction to Database Systems, 6th-ed., Page 650
  4. ↑ The AI Effect
  5. ↑ STLport: An Interview with A. Stepanov