Skip to main content

Class Attributes

In Object‑Oriented Programming, attributes (also called properties or fields) are the pieces of data that each object of a class holds. In EasyBite, attributes define the state of an object, and methods define its behavior. This document provides a very detailed, step‑by‑step guide to declaring, initializing, accessing, and managing class attributes in EasyBite—including how to use the this keyword to refer to the current object. Every concept is explained simply, with plenty of examples and outputs, so that even complete beginners can follow along.


Table of Contents


What Are Attributes?

  • Attributes are variables that belong to each object (instance) of a class.
  • They represent the state or data of an object.
  • Every instance has its own copy of each attribute declared in the class.

For example, a Person class might have attributes name and age. Each Person object stores its own name and age independently.


Declaring Attributes in a Class

Attributes are declared at the top of a class using set and a default value. The syntax is:

class ClassName
set attribute1 to defaultValue1
set attribute2 to defaultValue2
...
end class
  • attribute1, attribute2, etc. become part of every object created from ClassName.
  • You must supply a default value (e.g., an empty string "", a number 0, or an empty list []).

No output is produced by declaring attributes alone.


Default Values for Attributes

Defaults serve two purposes:

  1. Initialization: Ensure attributes have a known starting value.
  2. Type hinting: Indicate what kind of data the attribute holds (string, number, list, boolean).

Examples of defaults:

  • String: set title to ""
  • Number: set count to 0
  • List: set items to []
  • Boolean: set isActive to true

These defaults apply to every new object unless you override them in the constructor.


Instance Attributes vs. Class‑Level Attributes

Instance Attributes

  • Declared inside class ... end class.
  • Each object gets its own separate copy.
  • Modifying an attribute on one object does not affect others.

Class‑Level Attributes (Static)

  • EasyBite does not natively support static or class‑level attributes in the same way as some other languages.
  • All attributes you declare in the class block behave as instance attributes.
  • If you need shared data, you can declare a global variable outside the class.

Initializing Attributes in init

To set attributes based on constructor parameters, define an init block inside your class:

class Person
set name to ""
set age to 0

init(initialName, initialAge)
set name to initialName
set age to initialAge
end init
end class
  • init runs automatically when you create a new object with new.
  • You pass initialName and initialAge to new Person(...).
  • Inside init you assign those values to the attributes.

No output occurs until you use the object.


Accessing and Modifying Attributes

Once you have an object, you can read or write its attributes using dot notation:

set p to new Person("Alice", 30)

// Read an attribute
show("Name: " + p.name)
// Output:
// Name: Alice

// Modify an attribute
set p.age to 31
show("New age: " + p.age)
// Output:
// New age: 31
  • p.name returns the current value of name.
  • set p.age to 31 changes age on that specific object.

Using Attributes Inside Methods

Methods defined in the class can refer directly to attributes without qualification:

class Person
set name to ""
set age to 0

init(n, a)
set name to n
set age to a
end init

method describe()
show("Person: " + name + ", Age: " + age)
end method
end class

set p to new Person("Bob", 25)
p.describe()
// Output:
// Person: Bob, Age: 25

Inside describe(), name and age refer to that object’s attributes.


Using the this Keyword

EasyBite also supports the this keyword to explicitly refer to the current object. This can make code clearer, especially when attribute names shadow local variables or parameters.

Syntax

  • Inside methods or init, use this.attribute to refer to the object’s property.
  • Similarly, this.method() calls another method on the same object.

Detailed Examples

class Counter
set count to 0

init(start)
this.count to start
end init

method increment()
this.count to this.count + 1
end method

method display()
show("Current count is " + this.count)
end method
end class

set c to new Counter(5)
c.display()
// Output:
// Current count is 5

c.increment()
c.display()
// Output:
// Current count is 6

Here, using this.count makes it explicit that we’re modifying the object’s count attribute.


Inherited Attributes in Subclasses

When you create a subclass with inherit, it automatically gets all attributes from its parent:

class Employee inherit Person
set employeeId to ""
end class

set e to new Employee("Carol", 28)
show("Name: " + e.name)
// Output:
// Name: Carol

show("ID: " + e.employeeId)
// Output:
// ID:
  • Employee inherits name and age from Person.
  • It adds its own employeeId attribute.
  • If Person has an init, be sure to call super.init(...) in Employee’s init to initialize inherited attributes.

Attribute Naming Conventions

Follow EasyBite’s naming rules for attributes:

  • Lowercase names, optionally with underscores for readability.
  • No hyphens or spaces.
  • Descriptive but concise:
    • Good: user_name, total_count
    • Avoid: UserName, totalCount, user-name

Detailed Examples with Outputs

Example 1: Simple Person Class

class Person
set name to ""
set age to 0

init(n, a)
this.name to n
this.age to a
end init

method introduce()
show("Hi, I am " + this.name + " and I am " + this.age + " years old.")
end method
end class

set alice to new Person("Alice", 30)
alice.introduce()

Output:

Hi, I am Alice and I am 30 years old.

Example 2: Car Class with this and Multiple Attributes

class Car
set make to ""
set model to ""
set year to 0
set speed to 0

init(mk, mdl, yr)
this.make to mk
this.model to mdl
this.year to yr
end init

method status()
show("Car: " + this.year + " " + this.make + " " + this.model + "; Speed: " + this.speed + " km/h")
end method
end class

set car1 to new Car("Toyota", "Corolla", 2021)
car1.status()

Output:

Car: 2021 Toyota Corolla; Speed: 0 km/h

Then modify speed:

set car1.speed to 80
car1.status()

Output:

Car: 2021 Toyota Corolla; Speed: 80 km/h

Best Practices

  • Use this when method parameters or local variables share names with attributes.
  • Always provide sensible defaults for attributes.
  • Initialize attributes in init to override defaults.
  • Group related attributes at the top for readability.
  • Document each attribute with inline comments if needed.

Conclusion

Class attributes in EasyBite are the foundation of an object’s state. By declaring attributes with set ... to, initializing them in init, accessing them via object.attribute or this.attribute, and understanding inheritance, you can build clear, maintainable data models. Combined with methods and the this keyword, attributes enable you to write robust, real‑world applications using EasyBite’s beginner‑friendly OOP syntax. Happy coding!