0% found this document useful (0 votes)
12 views2 pages

Workshop 5

The document outlines the memory allocation process during program execution, detailing the roles of stack, static heap, and dynamic heap. It describes the creation and management of objects like BeeColony and FPTUniversity, including method calls and polymorphic behavior through the Role interface. Additionally, it explains the differences between abstract classes and interfaces, emphasizing the necessity of implementing abstract methods in subclasses.

Uploaded by

huudanh2366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Workshop 5

The document outlines the memory allocation process during program execution, detailing the roles of stack, static heap, and dynamic heap. It describes the creation and management of objects like BeeColony and FPTUniversity, including method calls and polymorphic behavior through the Role interface. Additionally, it explains the differences between abstract classes and interfaces, emphasizing the necessity of implementing abstract methods in subclasses.

Uploaded by

huudanh2366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Memory Map and Explanation

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.

Step-by-Step Execution and Memory Map


1. Program Starts (Main Method Invocation):
○ When the main method in Tester is called, the stack memory is used to store
method calls and local variables.
○ An instance of BeeColony (named obj1) is created. This calls the constructor
BeeColony(int, String, String), which in turn calls the constructor of its
superclass Colony. These constructors initialize the attributes like size, type,
and place for BeeColony.
2. Dynamic Heap Allocation for BeeColony Object:
○ obj1 (of type BeeColony) is allocated in the dynamic heap. It has a reference
to the BeeColony object stored in the stack as a local variable.
○ The fields size, type, and place are set with initial values.
○ Methods grow() and reproduce() are called on obj1. Each method
invocation adds to the stack temporarily and then is removed once execution of
each method completes.
3. Static Heap for Shared Information:
○ Static or class-level variables (not used in this program) would be stored in the
static heap. This includes any constants or methods that are not instance-
specific.
4. Dynamic Heap Allocation for FPTUniversity Object:
○ obj2 (of type FPTUniversity) is created with a call to FPTUniversity(int,
String, String). This allocates memory for the FPTUniversity object in
the dynamic heap and references it with a local variable in the stack.
○ obj2 has fields size, name, and address set, following the constructor chain
up to Organization.
○ Methods enroll() and educate() are called on obj2, and their data is
pushed to the stack temporarily during execution.
5. Polymorphic Behavior with Role Interface:
○ The variable df is declared as Role type, showcasing polymorphism.
○ df is first assigned a BeeColony object and later a FPTUniversity object,
both implementing the Role interface. This allows df to call createWorker()
on both objects, even though df is of type Role.

Questions and Explanations


1. What is stored in the static heap, stack, and dynamic heap?
○ Static Heap: Holds class-level data, like constants and static methods. Although
unused in this code, any shared class-level data would reside here.

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.

You might also like