Skip to main content

Class Relationships

Understanding Class Relationships in OOP (EasyBite)

In Object-Oriented Programming (OOP), classes do not exist in isolation. They often interact or relate to other classes in meaningful ways. Understanding these relationships helps you design clean, efficient, and modular software.

This page gives you a complete overview of the common types of class relationships with clear examples in EasyBite syntax.


1. Inheritance — “is-a” Relationship

Inheritance is when one class inherits properties and behaviors from another class. This is described as an "is-a" relationship. For example, a Dog is a Animal.

Example

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

class Dog inherit Animal
method bark()
show "Woof!"
end method
end class

set d to new Dog()
d.speak()
d.bark()

Note: That calling set d to new Dog() is like given birth to a new Dog i.e each time we use new keyword is like you are telling the animal to give birth to a new Dog.

Output

Animal speaks
Woof!

The Dog class inherits from Animal, meaning it gets the speak method for free.


2. Composition — “has-a” Relationship

Composition means that a class contains another class as a part of it. This is a "has-a" relationship. For example, a Car has a Engine.

Example

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

class Car
declare engine
init()
this.engine to new Engine()
end init

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

set c to new Car()
c.run()

Output

Engine started

The Car class has-a Engine. This is composition — the car is composed of an engine and other parts.


3. Aggregation — “has-a” (weaker than composition)

Aggregation is similar to composition, but the object being used is created outside and passed in. It still shows a "has-a" relationship but is more flexible and less tightly coupled.

Example

class Department
method showname()
show "Computer Science"
end method
end class

class University
declare dept
init(dept)
this.dept to dept
end init

method display()
dept.showname()
end method
end class

set dpt to new Department()
set uni to new University(dpt)
uni.display()

Output

Computer Science

In aggregation, the Department is passed into the University, showing a weaker ownership than composition.


4. Dependency — “uses-a” Relationship

A class depends on another if it uses it temporarily inside one of its methods. This is a "uses-a" relationship. It's the weakest form of connection.

Example

class Printer
method printdata(data)
show "Printing: " + data
end method
end class

class Report
method generate()
set p to new Printer()
p.printdata("Report content")
end method
end class

set r to new Report()
r.generate()

Output

Printing: Report content

Here, Report uses Printer, but only within the generate method. That’s a dependency.


5. Visual Summary of Class Relationships

+-------------------+
| Animal |
+-------------------+

| is-a (inheritance)
+-------------------+
| Dog |
+-------------------+


+-------------------+ +-------------------+
| Car | has-a → | Engine |
+-------------------+ +-------------------+


+-------------------+ +-------------------+
| University | has-a → | Department |
+-------------------+ +-------------------+
(aggregation)


+-------------------+ uses-a → +-------------------+
| Report | | Printer |
+-------------------+ +-------------------+
(dependency)

Summary Table

RelationshipDescriptionEasyBite FeatureOwnership Strength
is-aInheritanceinheritStrong
has-aCompositionMember object (created in class)Strong
has-aAggregationMember passed to constructorMedium
uses-aDependencyUsed temporarily in methodWeak

Conclusion

Understanding class relationships is essential for designing well-structured and maintainable programs. EasyBite gives you the tools to represent these relationships clearly:

  • Use inherit for is-a relationships (inheritance).
  • Use attributes and init for composition and aggregation.
  • Use temporary objects inside methods for dependencies.

This foundation helps you structure larger programs that are easier to update, test, and reuse.