Friday, September 24, 2010

Java Notes - Access Control and Declarations

Starting with this post, I will be posting notes regarding certain topics about the Java Programming Language. I  am going back over the topics and I think it is a good idea to keep notes of the most important concepts that a Java programmer must master.

The first topic on which I am posting notes is "Access Control and Declarations".
So let us get started.


Source File Declaration


First, we start with the rules for a Java source file: 
  • There can be only one public class per source code file.
  • Comments can appear at the beginning or end of any line in the source code file
  • If there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class Dog { } must be in a source code file named Dog.java.
  • If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.
  • If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file. If there are no package or import statements, the class declaration must be the first line in the source code file.
  • Import and package statements apply to all classes within a source code file. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports.
  • A file can have more than one nonpublic class.
Class Modifiers


First a little word on modifiers. 
Modifiers fall into two categories: 


 - Access Modifiers : public, private or protected. With these three access modifiers, four levels of access control can be reached. The fourth one is when none of the three modifiers is used during declaration. 
- Non-access Modifiers: strictfp (which we will not cover at all), final and abstract. 


This section deals with using modifiers when declaring classes. 


Default Access Class Declaration:  This level of access is achieved when none of the access modifiers (public, private or protected) is used when declaring the class.


package bmw;
class Vehicle { }


A default access level for a class means package-level access i.e. the class is visible only in the package in which it is declared. 
Example: The following code results with a compilation error


package boats;
import bmw.Vehicle; 
class SpeedBoat extends Vehicle { } 
Public Access Class Declaration:  Using the public modifier when declaring a class enables this class to be visible to all classes from all packages (still need to import if used from a different package).


Final Classes: The final non-access modifier is used when we want to make a certain class unable to be extended ( no other class can be inherited from this class). An example for this is the String class. 


Abstract Classes: When a class is marked with the abstract non-access modifier it means that this class can not be instantiated. The only purpose of such a class is to be extended. The methods that are defined as abstract end with a semicolon instead of curly braces. 


public abstract void becomeAbstract();



An important thing to note here is that if a class contains an abstract method then the whole class must be marked as abstract. 
Furthermore, an abstract class can contain non-abstract methods. This way, all the classes that extend the abstract class have inherited functionality and need to only implement the methods that are subclass specific. 


Interface


An interface is a contract that defines what a class can do. An interface is a 100% abstract class. That is to say all the methods in an interface are abstract. 

public interface Payable { }

The following rules apply:
  • All variables defined in an interface must be public, static, and final, in other words, interfaces can declare only constants, not instance variables. 
  • Interface methods must not be static. 
  • Because interface methods are abstract, they cannot be marked final, strictfp, or native. 
  • An interface can extend one or more other interfaces. 
  • An interface cannot extend anything but another interface. 
  • An interface cannot implement another interface or class.
  • An interface must be declared with the keyword interface. 

    No comments: