Visibility Modifier Flashcards
Types Of My Modifiers
We divide modifiers into two groups:
Access Modifiers - controls the access level
Non-Access Modifiers - do not control access level, but provides other functionality
Access Modifiers For clases
1.public
The class is accessible by any other class
2.default
The class is only accessible by classes in the same package. This is used when you don’t specify a modifier. You will learn more about packages in the Packages chapter
Access modifiers for
[Attributes,Method, Constructor]
public
The code is accessible for all classes
2.private
The code is only accessible within the declared class
3.default
The code is only accessible in the same package. This is used when you don’t specify a modifier. You will learn more about packages in the Packages chapter.
4.protected
The code is accessible in the same package and subclasses.
Non Access modifiers [For Classes]
1.final
The class cannot be inherited by other classes (You will learn more about inheritance in the Inheritance chapter)
2.abstract
The class cannot be used to create objects (To access an abstract class, it must be inherited from another class. You will learn more about inheritance and abstraction in the Inheritance and Abstraction chapters)
Non Access modifiers [For attributes and methods, you can use the one of the following]
1.final
Attributes and methods cannot be overridden/modified.
2.static
Attributes and methods belongs to the class, rather than an object
3.abstract
Can only be used in an abstract class, and can only be used on methods. The method does not have a body, for example abstract void run();. The body is provided by the subclass (inherited from). You will learn
4.transient
Attributes and methods are skipped when serializing the object containing them
5.synchronized
Methods can only be accessed by one thread at a time.
6.volatile
The value of an attribute is not cached thread-locally, and is always read from the “main memory”