The following keywords are used to define characteristics of the class when it is created:
- abstract - The class cannot be instantiated. It can only be subclassed. This is also used to createe methods that do not have implementations in the base class. Abstract methods may only be declared in a abstract class. The subclass must implement the method unless it is an abstract class also.
- class - Indicates a class declaration.
- extends - Used to identify the superclass that this class will be a subclass to.
- final - The class cannot be used to create subclasses.
- implements - Used to implement an interface to be used with the class.
- public - Allows the class to be used by any package. It is by default only available to the package it is contained in.
An example declaration:
public class MyClass extends MainClass
public MyClass() //Constructor
The methods and variables may be class methods or variables or instance methods or variables depending on how they are declared. If declared using the keyword "static" they are class variables or methods.
The system provides the constructor by default if it is not written into your class. Also you can write one or more additional constructors that may take various arguments. If you declare the object using a specific set of arguments, the appropriate constructor will be used to create the class. The line:
calls the constructor for the MyClass superclass which is MainClass (since the extends keyword declares it as a subclass to Mainclass). In the example above, the constructor is publically accessible. Constructor access specifiers:
- package - Only classes in the same package can declare an instance of your class.
- private - No other class can create an instance of your class
- protected - Only subclasses can create instances of the class.
- public - An instance of the class can be made by any class.
A constructor method has the same name as the class and return no type (void). There may be more than one class constructor with different parameter lists and one constructor may call another using the "this" keyword. The constructor for the superclass may be called using the keyword "super". The superclass constructor must be called from the first line in your subclass constructor. It is called by default.
Keywords and meanings:
- abstract - There is no implementation of this method and it must be a member of an abstract class.
- Access level - One of the following access levels are declared
- final - Subclasses cannot over ride this method.
- native - the method is implemented in a language other than Java.
- static - This is a class method and cannot be instantiated.
- synchronized - For threads that access the same data so the data is accessed safely when more than one thread may access it.
An additional keyword is throws which is used to declare an error type that the method may throw if an error occurs.
Objects or primitive data types may be passed to methods. When primitive type arguments are passed to a method, the value of the primitive such as an integer value is passed. An object is not passed. This means that the method the primitive type is passed to cannot change the primitive type's value. This is because it is an immediate value and not an actual variable. This value can only be read. If an object is passed to the method rather than a primitive data type, then any method in the passed object can change an attribute of that object.
Keywords for Reference
- this - Used to explicitly refer to a member variable or method. If there is a name clash such as if a passed variable has the same name as a member variable, this keyword is necessary. Thie keyword is also used to refer to methods and can be used to call a class constructor.
- super - Used to refer to member variables or methods of the superclass even if they were overridden in the subclass.