Skip to main content

Constructors

In Object‑Oriented Programming (OOP), a constructor is a special method that runs automatically when you create a new object. It allows you to set up the object’s initial state—assigning values to attributes, allocating resources, or performing any startup logic. In EasyBite, constructors are defined with the init keyword and closed with end init. This document provides a very detailed, step‑by‑step guide to constructors in EasyBite, with simple explanations, numerous examples, and clear outputs so that even complete beginners can follow along.


Table of Contents


What Is a Constructor?

A constructor is a method that:

  • Has the special name init.
  • Runs once when you call new ClassName(...).
  • Receives any arguments you pass to new.
  • Sets up the object’s attributes or performs startup tasks.

Without a constructor, every attribute uses its default value and no custom initialization occurs.


Why Use a Constructor?

Constructors let you:

  • Initialize attributes to meaningful values instead of defaults.
  • Validate input before the object is used.
  • Encapsulate startup logic (opening files, setting timers, registering events).
  • Ensure every object is in a valid state immediately after creation.

Using constructors promotes safer, cleaner, and more maintainable code.


Constructor Syntax in EasyBite

Within your class … end class block, define:

init(param1, param2,)
// initialization code
end init
  • Name: always init.
  • Parameters: comma‑separated list, just like methods.
  • Body: any valid EasyBite code.
  • Closure: must end with end init.

The arguments you pass to new ClassName(...) are forwarded to init.


Basic Example: No‑Argument Constructor

If you need no parameters, you can still define an init to run code on creation:

class Logger
set logs to []

init()
show("Logger initialized.")
end init

method log(message)
append(logs, message)
end method
end class

set logger to new Logger()
// Output:
// Logger initialized.

logger.log("First entry")

Here, creating logger prints a message immediately.


Parameterized Constructor

Most constructors accept parameters to set up attributes:

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

set alice to new Person("Alice", 30)
alice.introduce()
// Output:
// Name: Alice, Age: 30
  • new Person("Alice", 30) calls init("Alice", 30).
  • name and age are set before any method runs.

Using this in Constructors

You can use the this keyword to make it explicit you’re assigning to the object’s attributes:

class Rectangle
set width to 0
set height to 0

init(w, h)
this.width to w
this.height to h
end init

method area()
return this.width * this.height
end method
end class

set rect to new Rectangle(5, 4)
show("Area: " + rect.area())
// Output:
// Area: 20

Using this is especially helpful if constructor parameters share names with attributes.


Constructor with Default Values

You can define default values for constructor parameters by using to:

class Circle
set radius to 1

init(r to 1)
set radius to r
end init

method circumference()
return 2 * 3.14159 * radius
end method
end class

set c1 to new Circle()
show("Circumference: " + c1.circumference())
// Output:
// Circumference: 6.28318

set c2 to new Circle(3)
show("Circumference: " + c2.circumference())
// Output:
// Circumference: 18.84954

Omitting the argument uses the default 1.


Constructor and Inheritance

When a subclass inherits from a parent class, call the parent’s constructor inside the subclass’s init using parent.init(...):

class Animal
set species to ""

init(spec)
set species to spec
end init

method info()
show("Species: " + species)
end method
end class

class Dog inherit Animal
set name to ""

init(spec, nm)
parent.init(spec) // initialize inherited attribute
set name to nm
end init

method info()
super.info() // call Animal.info()
show("Name: " + name)
end method
end class

set d to new Dog("Canine", "Rex")
d.info()
// Output:
// Species: Canine
// Name: Rex
  • parent.init(spec) ensures the parent’s initialization logic runs.

Order of Execution

  1. Class definition loads attributes and methods (no runtime action).
  2. new ClassName(...) is called.
  3. Inherited init (if subclass, parent init via parent) runs first.
  4. Subclass init body runs.
  5. Object is returned, ready for use.

Knowing this order helps you structure initialization across class hierarchies.


Common Pitfalls

  • Missing end init: causes a syntax error.
  • Forgetting to call parent.init(...) in subclasses: inherited attributes remain at defaults.
  • Mixing up parameter order: arguments must match the init signature.
  • Referencing attributes before initialization: use this.attribute only after it’s set.

Best Practices

  • Always define an init even if it simply assigns defaults—this makes future extensions easier.
  • Validate constructor arguments (e.g., ensure numbers are positive).
  • Keep init short: delegate complex setup to private methods.
  • Document parameters with comments above init.
  • Use default values for common cases.

Detailed Examples with Outputs

Example 1: Bank Account Initialization

class BankAccount
set balance to 0

init(openingBalance to 0)
if openingBalance >= 0
set balance to openingBalance
else
set balance to 0
end if
show("Account opened with balance: " + balance)
end init

method getBalance()
return balance
end method
end class

set acct1 to new BankAccount()
// Output:
// Account opened with balance: 0

set acct2 to new BankAccount(500)
// Output:
// Account opened with balance: 500

set acct3 to new BankAccount(-100)
// Output:
// Account opened with balance: 0

Example 2: Configuration Loader

class Config
set settings to {}

init(path)
// pretend to read a file at path
set settings to { "debug": true, "version": "1.0.0" }
show("Loaded config from " + path)
end init

method get(key, default to nil)
if key in settings
return settings[key]
else
return default
end if
end method
end class

set cfg to new Config("/etc/app.conf")
// Output:
// Loaded config from /etc/app.conf

show("Debug: " + tostring(cfg.get("debug", false)))
// Output:
// Debug: true
show("Port: " + tostring(cfg.get("port", 8080)))
// Output:
// Port: 8080

Conclusion

Constructors (initend init) in EasyBite are the first code that runs when you create an object with new. They let you:

  • Set up attributes to meaningful values.
  • Validate input and maintain object integrity.
  • Chain initialization through inheritance with parent.init(...).
  • Provide defaults for flexible object creation.

By mastering constructors, you ensure every object starts life in a valid, predictable state—laying the foundation for robust, maintainable EasyBite applications. Happy coding!