Object-oriented programming (OOP) is a way of writing computer programs using "objects" to stand for data and methods. Often, computer programs that are not object-oriented are a list of instructions for the computer, telling it to do certain things in a certain way. This is called procedural programming. However, in object-oriented programming, computer programs use objects that talk to one another to change the data in those objects and to work in a way that the user wants. 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 are a mix of different ways of writing computer programs (called programming paradigms). 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 allow you to write computer programs in object-oriented programming. Some of these programming languages are: C++, Java, Ruby, Perl, Emarald, Sapphire, PHP, Python, C#, etc.

Features

[change | change source]

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

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

Examples

[change | change source]

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

[change | change source]

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(f"My name is {self.name}")
    def say_goodnight(self):
        if self.friend is None:
            print("Good night nobody.")
        else:
            print(f"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

[change | change source]

This code is in Java.

The Human class
public class Human {
    private String name; // the name of this Human
    private Human friend; // the Human's friend
    
    // This constructor creates a new Human object when given the name and friend
    public Human(String name, Human friend) {
        this.name = name;
        this.friend = friend;
    }
    
    // This constructor creates a new Human object when only given the name
    public Human(String name) {
        this(name, 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
public 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

[change | change source]

Even though object-oriented programming is popular, some people think that it is bad and criticize it.

References

[change | change source]
  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". www.stlport.org.