Skip to main content

OOP Frequently Asked Questions (FAQ)

What is OOP?

OOP stands for Object-Oriented Programming. It is a way of writing and organizing code that is inspired by real-world objects. In OOP, we use classes to define how objects should behave, and we create objects as instances of those classes.

OOP is used to structure software in a clear, reusable, and scalable way. It makes it easier to model complex things, organize related behavior and data together, and build programs that are easier to understand and maintain.


What is a class?

A class is a blueprint or template for creating objects. It defines the attributes (data) and methods (functions) that the objects created from the class will have.

class Person
method greet()
show "Hello!"
end method
end class

In this example, Person is a class. It has one method called greet.


What is an object?

An object is a real-world instance of a class. You create an object using the new keyword.

p to new Person()
p.greet()

Output

Hello!

What is a method?

A method is a function defined inside a class. Methods define the behavior of the object.

class Calculator
method add(a, b)
show a + b
end method
end class

c to new Calculator()
c.add(5, 7)

Output

12

What is a constructor?

A constructor is a special method named init. It is automatically called when an object is created using the new keyword. It is used to set up or initialize the object.

class Person
declare name

init(name)
this.name to name
end init

method greet()
show "Hello " + this.name
end method
end class

p to new Person("Jane")
p.greet()

Output

Hello Jane

What are access modifiers?

Access modifiers define who can access certain parts of a class. In EasyBite:

  • Use secret to make an attribute or method private, meaning it can only be accessed inside the class.
  • If you don’t use secret, then it is public by default and can be accessed from outside.
class Data
secret declare hidden
declare visible

init()
this.hidden to 42
this.visible to 100
end init

method showdata()
show this.visible
end method
end class

What is inheritance?

Inheritance allows one class to reuse the behavior and attributes of another class. You use the inherit keyword for this in EasyBite.

class Animal
method sound()
show "Some sound"
end method
end class

class Dog inherit Animal
method sound()
show "Bark"
end method
end class

d to new Dog()
d.sound()

Output

Bark

You can also call the parent's method using parent.methodname() or call the parent's constructor using parent.init().


What is encapsulation?

Encapsulation means bundling the data (attributes) and behavior (methods) together in a class and restricting access to the internal state. This is done using the secret keyword to protect data from outside interference.


What is abstraction?

Abstraction means focusing on the essential features while hiding complex internal details. For example, you can create a Car class that has a drive method without needing to show how the engine works internally.


What is polymorphism?

Polymorphism allows different classes to implement the same method in different ways.

class Animal
method speak()
show "Animal sound"
end method
end class

class Cat inherit Animal
method speak()
show "Meow"
end method
end class

class Cow inherit Animal
method speak()
show "Moo"
end method
end class

set animals to [new Cat(), new Cow()]

foreach a in animals
a.speak()
end foreach

Output

Meow
Moo

What is the difference between inheritance and composition?

  • Inheritance: One class extends another using inherit.
  • Composition: A class includes another class as part of its structure.

Example of composition:

class Engine
method start()
show "Engine starts"
end method
end class

class Car
declare engine

init(engine)
this.engine to engine
end init

method drive()
engine.start()
end method
end class

set e to new Engine()
set c to new Car(e)
c.drive()

Output

Engine starts

Are setter and getter functions built-in?

No. Just like in Java, EasyBite does not have built-in getter or setter functions. Instead, you create them manually using methods:

class Book
secret declare title

method settitle(t)
this.title to t
end method

method gettitle()
show this.title
end method
end class

b to new Book()
b.settitle("My Book")
b.gettitle()

Output

My Book