Object-oriented programming facts for kids
Object-oriented programming (OOP) is a way of writing computer programs. It uses special "objects" to organize information and actions. Think of it like building with Lego bricks. Each brick (object) has its own shape (data) and can connect in certain ways (methods).
In older ways of programming, called procedural programming, you give the computer a long list of step-by-step instructions. It's like a recipe where you follow each step exactly. But in OOP, you create objects that can talk to each other. These objects work together to change information and do what the user wants.
One great thing about OOP is that it helps programmers reuse their code. This means they don't have to write the same instructions over and over again. It makes building big programs easier and faster.
Many programming languages let you use object-oriented programming. Some popular ones include C++, Java, Python, Ruby, and PHP. Some languages, like Python, even let you mix OOP with procedural programming.
Contents
What is OOP?
The main idea behind object-oriented programming is that everything in your program can be thought of as an object. These objects can be different types, but they all have two main parts:
- Attributes (Variables): These are like the facts or details about an object. For example, if you have an object for a "car," its attributes might be its color, speed, or how many wheels it has. Attributes hold different kinds of information, like integers (whole numbers) or lists of items.
- Methods (Procedures): These are the actions an object can do. For a "car" object, methods could be "start engine," "accelerate," or "brake." Methods are lists of instructions that tell the computer to take information, do some calculations, or change data. Then, they give an output to the user.
A class is like a blueprint or a template for creating objects. It describes what attributes and methods an object of that type will have. For example, a "Car" class would define that all cars have a color and a speed, and they can all accelerate. An object is then a real item created from that blueprint. So, "my red car" would be an object (an instance) of the "Car" class. Classes often work together to make a program do what you want.
Simple Examples
Let's look at a simple example using a class called `Human`. This class will have attributes like `name` (for the person's name) and `friend` (for the name of their friend). The methods in the `Human` class will use these attributes to do things, like saying their name or saying goodnight.
Python Code Example
This code shows how the `Human` class works 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 Code Example
This code shows the same `Human` class in Java.
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);
}
}
}
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'
}
}
Some Criticisms
Even though object-oriented programming is very popular, not everyone agrees that it's the best way to program. Some people have pointed out a few things they see as downsides:
- Some experts believe that OOP doesn't always make programmers more productive than other ways of programming.
- It can sometimes be hard to agree on what "object-oriented programming" truly means, which makes it tricky to compare it to other programming styles.
- A few people think that OOP might limit how programmers think about solving problems, especially when it comes to math-related tasks.
- Some have even suggested that OOP might be used to keep average programmers from making big mistakes, but it could also slow down really good programmers who know faster ways to do things.
Images for kids
See also
In Spanish: Programación orientada a objetos para niños