ACCESS MODIFIER FOUR ACCESS MODIFIERS PRIVATE MODIFIER Methods

  • Slides: 11
Download presentation
ACCESS MODIFIER

ACCESS MODIFIER

FOUR ACCESS MODIFIERS

FOUR ACCESS MODIFIERS

PRIVATE MODIFIER �Methods, variables and constructors that are declared private can only be accessed

PRIVATE MODIFIER �Methods, variables and constructors that are declared private can only be accessed within the declared class itself. �Private access modifier is the most restricted access level, Class and Interface cannot be private. �Using the Private modifier is the main way that an object encapsulates itself and hide data from the outside world.

PRIVATE - EXAMPLE

PRIVATE - EXAMPLE

DEFAULT MODIFIER �Default access modifier has no keyword. �When there is no access modifier

DEFAULT MODIFIER �Default access modifier has no keyword. �When there is no access modifier declared, the access is default level access. �The classes marked with default access are visible only to the classes inside the same package.

DEFAULT - EXAMPLE

DEFAULT - EXAMPLE

PROTECTED MODIFIER �Methods, variables and constructors that are declared protected in a super class

PROTECTED MODIFIER �Methods, variables and constructors that are declared protected in a super class can be accessed only by subclasses in other package or any class within the package of the protected member’s class. �The protected access modifier cannot be applied to classes and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.

PROTECTED - EXAMPLE

PROTECTED - EXAMPLE

PUBLIC MODIFIER �A class, method, constructor, interface etc. . declared public can be accessed

PUBLIC MODIFIER �A class, method, constructor, interface etc. . declared public can be accessed from any other class. �Therefore fields, methods, blocks declared inside a public class can be access from any class belonging to the Java Universe. �If the public class we are trying to access is in a different Package, then the public class still in need to be imported.

PUBLIC - EXAMPLE

PUBLIC - EXAMPLE