UML Basics & Access Modifier / PDF
UML Basics & Access Modifier / PDF
The Access Modifiers -- public
The Access Modifiers -- public
npublic
access modifier: the + sign in UML
nFields,
methods and constructors declared public (least restrictive) within a public
class are accessible by all class in the Java program, whether these classes
are in the same package
or in another package.
nFor
simplicity, just take package
as the file folder in Windows
nNote:
in one application program, there is only and only one public class
nFields/methods
are called Class fields/method/member, i.e., you don’t need to create an
instance to use them; the convention is to use the classname.methodName notation
The Access Modifiers --
private
nprivate
access modifier: the – sign in UML
nThe
private (most restrictive) fields or methods cannot be accessed outside the
enclosing/declaring class. To make all fields private, provide public getter/setter methods for them.
nThe
getter is used to set the values of the data fields
nThe
setter is used to change/modifier the fields
The Access Modifiers --
protected
nprotected access modifier: the # sign in UML
nFields, methods and constructors declared protected in a class can be accessed only by subclasses in other packages and by those in the same
class even if they are not a subclass of the protected member’s class.
The Access Modifiers --
default
ndefault
access modifier: without any sign
nwhen
no access modifier is present, any class, field, method or constructor are accessible only by classes in the same package.
nThis
is also called the instance
members, i.e., must create an
instance of the class before the fields/methods can be used
nUse
the instanceName.method() format
UML Basics & Access Modifier / PDF
0 commentaires: