Jump to content

3.1.2 Object-Oriented Programming (OOP) - Basic Concepts

From Computer Science Knowledge Base

3.1.2 Object-Oriented Programming (OOP) - Basic Concepts

Imagine you're designing a video game where you have different characters: a knight, a wizard, and a dragon. Instead of just listing all their actions as separate steps, you can think of them as "objects."

Object-Oriented Programming (OOP) is a popular way to organize code around "objects", which are like digital versions of real-world things. Each object can have its own properties (things that describe it, like color, size, health) and behaviors (things it can do, like move, attack, cast a spell).

Key Ideas:

  • Objects: These are like individual items. For example, a specific "blue car" is an object.
  • Classes: This is like a blueprint or a cookie cutter for creating objects. A "Car" class would define what all cars have (like color, speed, number of wheels) and what all cars can do (like accelerate, brake). From this "Car" blueprint, you can create many different car objects (a blue car, a red car, a fast car).
  • Properties (or Attributes): These are the characteristics or data that an object has. For a "Car" object, properties might be color, speed, fuel_level.
  • Methods (or Behaviors): These are the actions an object can perform. For a "Car" object, methods might be accelerate(), brake(), turn().

How it Works: In OOP, you define these blueprints (classes) and then create many "instances" (objects) from those blueprints. When you want something to happen, you tell a specific object to perform one of its methods. This helps keep code organized and makes it easier to manage large programs.

Example:

  • Class: Dog
    • Properties: name (e.g., "Buddy"), breed (e.g., "Golden Retriever"), color (e.g., "golden"), age (e.g., 3)
    • Methods: bark(), fetch(item), wagTail()

If you have two Dog objects, "Buddy" and "Max," they both have name, breed, etc., and they both can bark(). But "Buddy" might have name = "Buddy" and "Max" might have name = "Max".

Many modern programming languages like Java, Python, C++, and C# are object-oriented.

Bibliography: