Workshop 5
Workshop 5
When the program runs, memory is allocated for objects and methods in different areas of
memory: stack, static heap, and dynamic heap. Here’s a breakdown of what happens step-
by-step and what each part of memory is used for.
1
○ Stack: Stores method calls and local variables. Each method invocation (like
main, enroll, etc.) creates a stack frame while it executes.
○ Dynamic Heap: Allocates memory for all instances (objects) created with new,
like instances of BeeColony, FPTUniversity, etc.
2. Why is the Organization class abstract?
○ Organization is abstract because it serves as a base class providing common
functionality (size and toString() method) but lacks specific implementation
details for communicateByTool(). Since Organization doesn’t define how
different organizations communicate, it leaves communicateByTool() as an
abstract method, to be implemented by subclasses.
3. Why must the Colony/University class implement the communicateByTool()
method?
○ Since communicateByTool() is declared abstract in the Organization
class, any non-abstract subclass (like Colony and University) must provide a
concrete implementation of this method. This ensures that all subclasses define
their own way of communication, fulfilling the contract established by
Organization.
4. Explain the polymorphism feature in your code.
○ Polymorphism is showcased through the Role interface. The variable df is
declared as type Role and can hold references to both BeeColony and
FPTUniversity objects, which implement Role. When df.createWorker()
is called, the correct createWorker() method is executed based on the
object’s actual type (BeeColony or FPTUniversity), allowing dynamic method
resolution.
5. Describe the difference between an interface and an abstract class.
○ Abstract Class:
■ Can contain both abstract methods (without a body) and concrete
methods (with a body).
■ Allows fields (variables) and constructors.
■ In Java, a class can extend only one abstract class.
○ Interface:
■ Contains only abstract methods (until Java 8, when default and static
methods were introduced).
■ Does not allow fields, except for constants (implicitly public static
final).
■ A class can implement multiple interfaces, allowing more flexibility with
multiple inheritance.