Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Java Programming - Encapsulation Activity

Java Programming - Encapsulation Activity Activity for observing program behavior for public vs private access modifiers.

Uploaded by

gojm.ins
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Java Programming - Encapsulation Activity

Java Programming - Encapsulation Activity Activity for observing program behavior for public vs private access modifiers.

Uploaded by

gojm.ins
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ICT 12 - Computer Programming January 16, 2025

Alternative Learning Activity

OOP Exercise - Observing Access Modifiers


Today, we will explore access modifiers in Java by running and observing a
program in two distinct scenarios. Follow the specific instructions for each
scenario carefully.

Scenario 1: Using public Access Modifier

1. Create a Person class:

 Add a name attribute/variable with the public access modifier. See


sample code below.

Figure 1.0: Person class with public name attribute.

2. Create a Main class:

 In the main method, create an object of the Person class.


 Access and print the name attribute of the person object in your
main method.

3. Run the program:

 Observe the behavior of the program. Screenshot the output for


scenario one.

Scenario 2: Using private Access Modifier

1. Modify the Person class:

 Change the name attribute's access modifier from public to


private.See sample code below.

Figure 2.0: Person class with public name attribute.


2. Copy your Main class from scenario one:

 Don’t change any code inside your Main class from scenario one,
and copy it for scenario two.

3. Run the program:

 Observe what happens when you try to access a private attribute


directly. Screenshot the output for scenario two, note any
compilation errors or messages the program generates.

End-of-Activity Questions:

1. What was your observation in Scenario 1 (using the public access modifier)?
 Explain how the name attribute was accessed and modified. What
output did you observe when you ran the program?

2. What was your observation in Scenario 2 (using the private access


modifier), and why do you think it caused an error?
 Describe what happened when you tried to access the name attribute
directly. What error did you encounter, and why do you think it
occurred?

3. In your own words, what is the purpose of a private access modifier in Java?
 Explain why it is important to use the private modifier for attributes in
object-oriented programming and how it helps with controlling access
to the data.

A Google Forms link has been sent to our group chat as a platform for you to
submit your answers. Please use the form to respond to the questions about
the code observations and the purpose of the private access modifier.

THANK YOU AND STAY SAFE!

You might also like