Computer Science
Grade 9
20 min
OOP Principles
OOP Principles
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define the concepts of a class and an object.
Explain the principle of Encapsulation using a real-world analogy.
Describe the principle of Inheritance and how it promotes code reuse.
Identify an example of Polymorphism.
Write a simple class definition with attributes and methods in pseudocode.
Create an object (instance) from a class.
Ever wondered how video games create thousands of different enemies or characters that all share some common traits but also have unique abilities? 🤔 Let's find out!
This lesson introduces Object-Oriented Programming (OOP), a powerful way to organize code. We will learn the core principles that help programmers build complex, reusable, and easy-to-manage software by thinking about code in terms of real-world objects....
2
Key Concepts & Vocabulary
TermDefinitionExample
ClassA blueprint or template for creating objects. It defines a set of properties (attributes) and behaviors (methods) that all objects of that type will have.A `Car` class could define that all cars have a `color` and a `max_speed` (attributes), and can `start_engine()` and `drive()` (methods).
ObjectA specific instance of a class. It's the actual thing created from the blueprint, with its own specific values for its attributes.If `Car` is the class, a red Ferrari is an object, and a blue Honda is another object. Both are cars, but they are separate instances with different colors.
EncapsulationThe bundling of data (attributes) and the methods that operate on that data into a single unit (a class). It also involves restricting direct access to some of an object...
3
Core Syntax & Patterns
Class Definition Syntax
CLASS ClassName:
ATTRIBUTE attribute_name = initial_value
METHOD method_name(parameters):
// code to perform an action
Use this pattern to create a blueprint for your objects. `CLASS` is the keyword, followed by a capitalized name. Inside, you define the data (attributes) and behaviors (methods) for the class.
Object Instantiation
variable_name = ClassName()
This is how you create an actual object from a class blueprint. The `ClassName()` part calls the class's constructor to build a new instance, which you then store in a variable.
Method Calling
object_variable.method_name(arguments)
Once you have an object, you can make it perform its actions by calling its methods. Use the object's variable name, a dot (.), the method na...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A `Wizard` class inherits from the `Player` class (which has `name`, `health`, and `take_damage()`). The `Wizard` class adds a new method, `cast_spell()`. Which of the following statements is FALSE?
A.`Wizard` object will have a `health` attribute.
B.`Wizard` object can call the `take_damage()` method.
C.`Player` object can call the `cast_spell()` method.
D.`Wizard` object can call the `cast_spell()` method.
Challenging
A programmer creates a `BankAccount` class. They make the `balance` attribute inaccessible from outside the class. The only way to change the balance is by calling the public `deposit()` and `withdraw()` methods. This practice of protecting data is a key part of which OOP principle?
A.Inheritance, because it inherits from a `Money` class.
B.Polymorphism, because `withdraw()` can mean different things.
C.Encapsulation, because it restricts direct access to the object's data.
D.Instantiation, because an account must be created.
Challenging
Given the following pseudocode, what is the final value of `my_bot.energy`? `CLASS Robot: ATTRIBUTE energy = 100; METHOD use_power(amount): self.energy = self.energy - amount; my_bot = Robot(); my_bot.use_power(20); my_bot.use_power(45);`
A.100
B.80
C.35
D.55
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free