I just started the java certification book and already I see that I know much less than I thought. I see modifiers PRIVATE for method or variable, PROTECTED for method or variable, and PUBLIC for class, method or variable.
the uppercase are mine but, does it mean we cannot have a PROTECTED class? or does it mean if we say nothing it means PROTECTED? Or does it not matter? and why?
I guess I'm confused with just the second page. anybody can help me?
Monica, You are partly correct in understanding that
PRIVATE for method or variable, PROTECTED for method or variable, and PUBLIC for class, method or variable
PRIVATE - Private variables are only visible from within the same class as they are created.in. This means they are NOT visible within sub classes. Private classes would always be nested.
PUBLIC - public class has global scope, and an instance can be created from anywhere within or outside of a program.
PROTECTED (classes would be nested) -A protected variable is visible within a class, and in sub classes, the same package but not elsewhere. Any class in the same directory is considered to be in the default package, and thus protected classes will be visible.
DEFAULT - A variable defined with no access modifier is said to have default visibility. Default visibility means a variable can be seen within the class, and from elsewhere within the same package, but not from sub-classes that are not in the same package(this is the major difference between DEFAULT and PROTECTED)