Explore how objects store data through attributes and perform actions through methods.
If you were designing a digital pet, how would the computer 'remember' if it was hungry or happy, and how would it know exactly how to wag its tail when you click 'play'?
In Object-Oriented Programming (OOP), an object is more than just code; it is a digital entity with a 'memory.' This memory is called the State. We define the state using Attributes (also known as instance variables). Think of attributes as the nouns of your object. For a `Player` object in a game, attributes might include `health`, `mana`, or `coordinateX`. These variables live as long as the object exists. Because each object is a unique instance of a class, two different `Player` objects can have different states—one might have while another has .
Let's define a `Rectangle` class. Its state is determined by its dimensions. 1. Declare the class: `class Rectangle`. 2. Define attributes: `double width` and `double height`. 3. When we create an instance, we assign values: `rect1.width = 5.0` and `rect1.height = 10.0`.
Quick Check
If a 'Car' class has a variable called 'currentSpeed', is that an attribute or a method?
Answer
It is an attribute, because it represents a piece of data (the state) rather than an action.
If attributes are the nouns, Methods are the verbs. Methods define the Behavior of an object—what it can actually do. A method is a function defined inside a class that has access to the object's attributes. Methods often change the state of the object. For example, a `takeDamage()` method would decrease the `health` attribute. By bundling data (attributes) and logic (methods) together, we achieve Encapsulation, making our code modular and easier to debug.
Quick Check
Why do we put logic like 'calculateArea' inside the class instead of just doing the math in the main program?
Answer
To ensure encapsulation; the object should be responsible for its own data and logic, making the code more reusable.
When writing methods, a common conflict arises: what if a method parameter has the same name as an attribute? To solve this, we use the this keyword (in Java/C++) or self (in Python). These keywords act as a self-reference, pointing to the specific instance of the object currently executing the code. It tells the computer, 'I am talking about my variable, not the temporary one you just handed me.' This is crucial for Disambiguation and ensures that the correct object's state is updated.
Imagine a `BankOrder` class with an attribute `amount`. We want to update it using a method. 1. Method signature: `void setAmount(double amount)`. 2. Inside the method, `amount = amount` is ambiguous. The compiler thinks you are assigning the parameter to itself. 3. Correct usage: `this.amount = amount`. This explicitly assigns the parameter value to the object's instance variable.
Which of these best describes the 'State' of a Smartphone object?
In the line 'this.weight = weight;', what does 'this.weight' refer to?
Methods can access and modify the attributes of the object they belong to.
Review Tomorrow
In 24 hours, try to explain the difference between a 'Class' and an 'Instance' to a friend, using the 'State and Behavior' of a video game character as an example.
Practice Activity
Create a 'SmartThermostat' class. Give it an attribute for 'targetTemperature' and a method 'adjustTemp(double amount)' that uses the 'this' keyword.