Skip to main content

Object‑Oriented Programming (OOP)

Object‑Oriented Programming (OOP) is a paradigm that organizes code around objects—self‑contained units that bundle data (attributes) and behavior (methods) together. Rather than thinking of a program as a sequence of instructions, you think of it as a collection of interacting objects, each responsible for its own internal state and actions. This leads to code that is:

  • Modular: Each object is a self‑contained building block.
  • Readable: Classes and objects map directly to real‑world concepts.
  • Reusable: You can create multiple instances from one class or extend classes via inheritance.
  • Maintainable: Changes to one class have minimal impact on others.

EasyBite’s OOP syntax is designed for beginners, offering clear, simple constructs for classes, objects, methods, and the core OOP principles without overwhelming complexity. This guide provides a very detailed and very long walkthrough of OOP in EasyBite, complete with numerous examples and outputs, so that even complete newcomers can follow along confidently.


Table of Contents


Why OOP?

As your programs grow, a flat structure of functions and global variables quickly becomes hard to manage. OOP solves this by:

  1. Grouping related data and behavior into classes, making it clear which functions operate on which data.
  2. Mapping software design to real‑world models, improving understanding and communication.
  3. Promoting reuse: you can instantiate many objects from the same class or extend an existing class to create a new variant.
  4. Isolating changes: modifications inside one class seldom break code elsewhere.

This makes OOP an essential tool for writing large, robust, and maintainable applications.


Core Concepts

Classes

A class is the blueprint for objects. It defines:

  • Attributes: pieces of data each object will hold.
  • Methods: functions that define the object’s behavior.

Syntax:

class ClassName
set attribute1 to initialValue1
set attribute2 to initialValue2

method methodName(parameters)
// method body
end method

method anotherMethod()
// ...
end method
end class
  • class begins the definition.
  • set declares and initializes attributes inside the class.
  • method defines a behavior; close methods with end method.
  • Close the class with end class.

No output is produced by merely defining a class.


Instances (Objects)

An instance is a concrete object created from a class. In EasyBite, you use the new keyword:

set myObject to new ClassName()

Here, myObject has its own copy of all attributes defined in ClassName. You can create as many instances as you like:

set car1 to new Car()
set car2 to new Car()

Each instance maintains its own state independently.


Attributes (Properties)

Attributes (also called properties or fields) are variables tied to each object. In the Car class example, color and speed are attributes:

class Car
set color to ""
set speed to 0
// ...
end class

To read or modify:

set car1 to new Car()
set car1.color to "red"
show("Car color is " + car1.color)

Output:

Car color is red

Methods (Behaviors)

Methods are functions defined inside a class that operate on the object’s attributes or perform actions:

class Car
// attributes...
method start()
show("Car started.")
end method
end class

set myCar to new Car()
myCar.start()

Output:

Car started.

Methods can take parameters and return values. They implicitly receive the instance they act on.


The Four Pillars of OOP

Encapsulation

Encapsulation bundles an object’s data and methods, controlling access to its internal state. In EasyBite, all attributes are public by default, but you encapsulate by:

  • Providing methods for any interaction with attributes.
  • Avoiding direct manipulation of attributes outside the class.

Example

class BankAccount
set balance to 0

method deposit(amount)
if amount > 0
set balance to balance + amount
end if
end method

method getBalance()
return balance
end method
end class

set acct to new BankAccount()
acct.deposit(150)
show("Balance: " + acct.getBalance())

Output:

Balance: 150

External code never changes balance directly, preserving valid state.


Abstraction

Abstraction hides complex implementation details and exposes only a simple interface. Class users need not understand internal workings.

Example

class Timer
set remaining to 0

method start(seconds)
set remaining to seconds
// Internal countdown logic is hidden
end method

method isTimeUp()
return remaining <= 0
end method
end class

set t to new Timer()
t.start(5)
// (No visible complexity; user simply checks t.isTimeUp())

Inheritance

Inheritance allows a subclass to derive from a parent class, inheriting its attributes and methods, while adding or overriding behavior.

Example

class Animal
method speak()
show("An animal makes a sound.")
end method
end class

class Dog inherit Animal
method speak()
show("Dog barks.")
end method
end class

set a to new Animal()
a.speak()
set d to new Dog()
d.speak()

Output:

An animal makes a sound.
Dog barks.

Polymorphism

Polymorphism (“many forms”) lets you treat different subclasses uniformly through a common interface:

set creatures to [ new Animal(), new Dog() ]
set i to 1
repeat while(i <= creatures.length())
creatures[i].speak()
set i to i + 1
end repeat

Output:

An animal makes a sound.
Dog barks.

Functions operating on Animal objects automatically adapt to Dog instances.


Basic OOP Syntax in EasyBite

class ClassName
set attribute1 to initialValue1
set attribute2 to initialValue2

method methodName(param1, param2)
// body
end method

method anotherMethod()
// ...
end method
end class
  • Use PascalCase for class names (Car, BankAccount).
  • Define attributes with set at the top.
  • Define behaviors with method/end method.
  • Instantiate with new ClassName().

Constructors and new

A constructor runs automatically when you create an object, initializing attributes. Named constructor in EasyBite:

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 p to new Person("Eve", 22)
p.introduce()

Output:

Name: Eve, Age: 22

You pass initial values directly to new Person(...), and the constructor sets up the object.


Detailed Examples with Outputs

Example 1: Person Class

class Person
set name to ""
set age to 0

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

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

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

Output:

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

Example 2: Vehicle Inheritance

class Vehicle
set speed to 0

init(initialSpeed)
set speed to initialSpeed
end init

method status()
show("Speed: " + speed)
end method
end class

class Car inherit Vehicle
set fuel to 100

init(initialSpeed, initialFuel)
parent.init(initialSpeed)
set fuel to initialFuel
end init

method refuel(amount)
set fuel to fuel + amount
show("Fuel level: " + fuel)
end method
end class

set myCar to new Car(50, 80)
myCar.status()
myCar.refuel(20)

Output:

Speed: 50
Fuel level: 100

Why OOP Matters in Practice

  • Scalability: Large systems break down into manageable classes.
  • Collaboration: Teams work on different classes without interference.
  • Maintainability: Fixing bugs or adding features is localized to specific classes.
  • Reusability: Generic classes (e.g., List, Timer) can be shared across projects.