# data[].attributes.body
Rayan

# data[].attributes.body
Fatima Waleed 11 CAI

# data[].attributes.body
public class Snack {

private String name;

private int numCalories;

// Default constructor

public Snack() {

name = "Unknown";

numCalories = 0;

}

// Parameterized constructor

public Snack(String name, int numCalories) {

this.name = name;

this. numCalories = numCalories;

｝

｝

# data[].attributes.body
//* Mariam Alshamsi

11 CAI

6098638*/

public class Snack {

private String name;

private int numCalories;

}

//default constructor

public Snack() {

this.name = "";

this.numCalories = 0;

}

//parameterized constructor

public Snack(String name, int numCalories) {

this.name = name;

this.numCalories = numCalories;

}

# data[].attributes.body
Public: can be accepted by anyone. Private: can be accessed by only some limited people

# data[].attributes.body
Maitha naser alreqabi

Private (inaccessible) and public(accessible)

# data[].attributes.body
public - members are accessible from outside the class. private - members cannot be accessed (or viewed) from outside the class.

Salama Ali

# data[].attributes.body
Naseem

Public and private

# data[].attributes.body
In Java, access and visibility constraints, also known as access modifiers, control how different parts of your code can interact with each other.

Public: This is like the front door, open to everyone. Any part of your program can access public members.

Private: This is like a bedroom, accessible only to the owner of the room (the class itself).

Rayan

# data[].attributes.body
Mariam Alshamsi

1. public and private

# data[].attributes.body
public and private

# data[].attributes.body
Fatima Waleed 11 CAI

Access and Visibility Constraints in Java

Access and visibility constraints in Java control who can access classes, methods, and variables using access modifiers:

1. public: Accessible everywhere.

2. protected: Accessible within the same package and in subclasses.

3. default (no modifier): Accessible only within the same package.

4. private: Accessible only within the same class.

# data[].attributes.body
Ayesha Waleed Alhammadi

Q1.1:

Public: Can be accessed by any class.

Private: Can only be accessed within the same class.

Protected: Can be accessed within the class, its subclasses, and same package.

Default: Can only be accessed within the same package.

Q1.2:

Making instance variables private means they can’t be accessed directly from outside the class. This is called encapsulation. Instead, you use special methods (called getters and setters) to access or change the data.