Computer Science, asked by vikash7828, 6 months ago

If an attribute is private, which methods
have access to it?

Select one:
A. Only those defined in the same
class.

B. Only classes in the same
package

C. Only instance methods in the
same class.

D. Only static methods in the same
class.​

Answers

Answered by hasanjamey
3

Answer: only instance methods in the same class .

option : (c)

Answered by syed2020ashaels
0

Answer:

Explanation:

Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself. Private access modifier is more secure and restrictive access level, whereas class and interfaces cannot be private.

Private Variables can be only be accessed outside the class if public getter methods are present in the class.

Using the private modifier object encapsulates itself from the outside world and restrict the access to it.

Example:

The following class uses private access control:

public class Priv {

private String format;

public String getFormat() {

return this.format;

}

public void setFormat(String format) {

this.format = format;

}

}

The format variable is private, so there's no way for other classes to retrieve or set its value directly. To make this variable accessible the define of public methods like getFormat(), which returns the value of format, and setFormat(String), which sets its value.

See more:

https://brainly.in/question/40162588

#SPJ2

Similar questions