Computer Science Grade 9 20 min

Attributes and Methods: Defining Object Behavior

Learn how to define attributes (data) and methods (functions) within a class.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define 'attribute' and 'method' in the context of Object-Oriented Programming. Identify the attributes (data) and methods (behaviors) for a real-world object. Write the code for a simple class with attributes in its constructor. Write the code for methods inside a class that define an object's actions. Create an object (instance) from a class. Use dot notation to access an object's attributes and call its methods. Think about your favorite video game character. What information describes them (name, health points) and what actions can they perform (jump, attack)? 🎮 In this lesson, you will learn how to build blueprints for objects using classes. We will focus on defining an object's characteristics (attributes) and its...
2

Key Concepts & Vocabulary

TermDefinitionExample AttributeA piece of data that describes a characteristic of an object. Attributes are like variables that belong to a specific object.For a 'Car' object, attributes could be 'color', 'brand', and 'current_speed'. MethodA function that belongs to an object and defines its behavior or actions. Methods are what an object can *do*.For a 'Car' object, methods could be 'start_engine()', 'accelerate()', and 'brake()'. ClassA blueprint or template for creating objects. It defines the attributes and methods that all objects of that type will have.The 'Car' class is the blueprint that describes what every car object will look like and how it will behave. Object (or Instance)A specific ent...
3

Core Syntax & Patterns

Defining a Class with Attributes class ClassName: def __init__(self, param1, param2): self.attribute1 = param1 self.attribute2 = param2 Use the 'class' keyword to start. The '__init__' method is a special constructor that runs when a new object is created. It's used to set the initial values of an object's attributes. Defining a Method class ClassName: # ... __init__ method ... def method_name(self, parameter): # code that performs an action self.attribute1 = 'new value' Methods are defined inside a class just like functions, using 'def'. The first parameter must always be 'self', which gives the method access to the object's own attributes. Accessing Attributes a...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
You are designing a class to model a `Book`. Which of the following is the most logical design for its attributes and methods?
A.Attributes: `read_page`, `close_book`. Methods: `title`, `author`.
B.Attributes: `title`, `author`, `current_page`. Methods: `turn_page()`, `go_to_page()`.
C.Attributes: `Book`. Methods: `create_book()`.
D.Attributes: `turn_page`, `go_to_page`. Methods: `title()`, `author()`.
Challenging
Why is the `self` parameter essential in instance methods? What would be the direct consequence of omitting it from the definition, like `def take_damage(amount):`, and then trying to access `self.health`?
A.The code would work, but it's bad practice. `self` is optional.
B.It would cause a `NameError` because `self` is not defined, so `self.health` cannot be accessed.
C.It would modify the `health` of all `Player` objects at once instead of just one.
D.Python would automatically add `self`, so there would be no consequence.
Challenging
A `Player` can level up. This action should increase their `level` by 1 and fully restore their `health` to 100. Given the code below, what will be the values of `hero.level` and `hero.health` after `hero.level_up()` is called? class Player: def __init__(self, name): self.name = name self.level = 5 self.health = 75 def level_up(self): self.level += 1 self.health = 100 hero = Player('Hero') hero.level_up()
A.level will be 5, health will be 75
B.level will be 6, health will be 75
C.level will be 6, health will be 100
D.level will be 5, health will be 100

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Object-Oriented Programming: Designing with Classes and Objects

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.