MODIFIERS 2

Protected

Only variables and methods may be declared protected. A protected feature of a class is available to all classes in the same package, just like a default feature. However, a protected feature of a class is available to all subclasses of the class that owns the protected package.

All java source files are related to a "package", when you work with files within a single working directory, you work under a "default" package, that is distinct from other packages, such as my_dog_package in this case.

If the class Dog was within the Canine.java source file, the class Dog would belong to the "default" package. In order for you to import the Dog class from a "foreign" package, you must declare the class public. But that's not all, let's see a more complex example:

Special Modifiers

final

Final features may not be changed. A final class may not be subclassed, a final variable may not be modified and a final method can't be overridden. Thus, final features only apply to:

classes abstract

This modifier may be applied only to:

An abstract class may not be instantiated. An abstract method can't be invoked, a subclass must override it and provide a non-abstract version of it.


 

The compiler insists that a class must be abstract if:

Abstract is the opposite of final. A final class, for example, may not be subclassed; an abstract class must be subclassed.

static

The static features belong to a class and are not associated with the instance of the given class. They may applied to:

Basic rules for static methods:

0 comments:

Post a Comment