Tutorial Description - Declaring classes, variables, and methods
Now let's look at ways we can modify classes, methods, and variables. There are two kinds of modifiers -- access modifiers and non-access modifiers. The access modifiers allow us to restrict access or provide more access to our code.
Declaring classes, variables, and methods page 2 of 5
Now let's look at ways we can modify classes, methods, and variables. There are two kinds of modifiers -- access modifiers and non-access modifiers. The access modifiers allow us to restrict access or provide more access to our code.
Class modifiers
The access modifiers available are public, private, and protected. However, a top-level class can have only public and default access levels. If no access modifier is specified, the class will have default access. Only classes within the same package can see a class with default access. When a class is declared as public, all the classes from other packages can access it.
Let's see the effect of some non-access modifiers on classes. The final keyword (see Java keywords and identifiers for more on keywords) does not allow the class to be extended. An abstract class cannot be instantiated, but can be extended by subclasses: