Classes and Objects
Classes and objects are at the heart of Object‑Oriented Programming (OOP). In EasyBite, classes let you define new data types—blueprints for objects—while objects are instances of those blueprints, each holding its own data and behavior. This document provides a very detailed, step‑by‑step guide to defining classes, creating objects, using constructors, and working with inheritance in EasyBite. Every concept is explained in simple terms with multiple examples and outputs so that even complete beginners can follow along.
Table of Contents
What Is a Class?
A class is a blueprint or template that describes:
- Attributes: named pieces of data every object of the class will have.
- Methods: named functions that every object of the class can perform.
Classes allow you to model real‑world concepts (like Car
, Person
, BankAccount
) in code. Once you’ve defined a class, you can create many objects (instances) of that class, each with its own attribute values.
Defining a Class
To define a class in EasyBite, use the class
keyword, list attributes and methods inside, then close with end class
. Here’s the basic structure:
class ClassName
set attribute1 to initialValue1
set attribute2 to initialValue2
method methodName(param1, param2)
// method body
end method
// You can define more methods here
end class
class ClassName
begins the definition.set attribute to value
declares an attribute and gives it a default.method … end method
defines a behavior.end class
closes the class.
Defining a class does not produce any runtime output by itself.
Attributes (Properties)
Attributes (also called properties or fields) hold an object’s data. They are declared with set
inside the class:
class Person
set name to "" // default empty string
set age to 0 // default zero
end class
- Every
Person
object will have its ownname
andage
. - You can choose sensible defaults or leave strings empty and numbers zero.
Methods (Behaviors)
Methods are functions that operate on an object’s attributes or perform actions. You define them with method
and close with end method
:
class Person
set name to ""
set age to 0
method introduce()
show("Hello, my name is " + name + " and I am " + age + " years old.")
end method
end class
- Inside a method, you can reference attributes directly by name.
- Methods can accept parameters and use
return
to output values.
Constructors with init
/ end init
A constructor is a special method that runs automatically when you create a new object. In EasyBite, the constructor is named init
and closed with end init
:
class Person
set name to ""
set age to 0
init(initialName, initialAge)
set name to initialName
set age to initialAge
end init
method introduce()
show("Name: " + name + ", Age: " + age)
end method
end class
init(params)
is called immediately when you instantiate the class.- Use
init
to assign initial values to attributes. - You still close the constructor with
end init
.
No output occurs until you create and use an instance.
Creating Objects with new
To create an object (instance) from a class, use the new
keyword:
set alice to new Person("Alice", 30)
- This calls the
init
constructor with arguments"Alice"
and30
. - The result is stored in the variable
alice
.
Once created, you can interact with the object:
alice.introduce()
Output:
Name: Alice, Age: 30
Accessing Attributes and Calling Methods
You can read and write attributes on an object using dot notation, and call methods the same way:
set bob to new Person("Bob", 25)
// Access attribute
show("Bob's age: " + bob.age)
// Output:
// Bob's age: 25
// Modify attribute
set bob.age to 26
show("Bob is now " + bob.age + " years old.")
// Output:
// Bob is now 26 years old.
// Call method
bob.introduce()
// Output:
// Name: Bob, Age: 26
object.attribute
reads or writes data.object.method()
invokes behavior.
Inheritance with inherit
Inheritance lets one class build upon another. Use the inherit
keyword instead of is
:
class Employee inherit Person
set employeeId to ""
set position to ""
init(name, age, id, positionTitle)
parent.init(name, age) // call Person's constructor
set employeeId to id
set position to positionTitle
end init
method introduce()
// extend Person's introduce
show("Employee ID: " + employeeId + ", Position: " + position)
end method
end class
class Subclass inherit Superclass
begins the subclass definition.- Inside the subclass’s
init
, callsuper.init(...)
to initialize inherited attributes. - You can override methods or add new ones.
Instantiate an Employee
:
set emp to new Employee("Carol", 28, "E123", "Engineer")
emp.introduce()
Output:
Name: Carol, Age: 28 years old.
Employee ID: E123, Position: Engineer
(Note: if you override introduce
, you may need to call parent.introduce()
first to show the name and age.)
Best Practices and Tips
- PascalCase for class names:
Person
,BankAccount
,Vehicle
. - Lowercase for attributes and methods:
name
,introduce()
. - Always define an
init
if you need to set up attributes—avoids forgetting to initialize. - Use
parent.init(...)
in subclasses to ensure parent attributes get initialized. - Keep methods focused: one behavior per method.
- Document your classes with comments before the
class
keyword to explain purpose.
Conclusion
In EasyBite, classes and objects give you a powerful way to model complex systems:
- Define a class with
class … end class
. - Declare attributes with
set
and behaviors withmethod … end method
. - Initialize objects using
init … end init
and create them withnew ClassName(...)
. - Extend functionality via inheritance using
inherit
.
By mastering these building blocks, you can structure your code in a clear, modular, and maintainable way. Happy coding!