Appendix C: Classes in Java


Creating a new class in Java requires the use of the class keyword:

  Modifier  class ClassName [extends SuperClass] [implements Interface] { } 

If a class is defined as part of a package, then its full name is package- name.classname . If it is not in a package, then the full name is classname . This information is important for instantiating and using classes that have already been defined (by Sun or a third party).

The Modifier may be one or more of the following:

Modifier

Meaning

Public

The class can be accessed by any other class, even outside its package (if it has one). If left off, then only classes in the same package can access the class.

Final

The class cannot be subclassed (derived from). A class with any final member functions is not required to be considered final itself.

Abstract

The class cannot be instantiated , and must be used as a super class (base class). If a class defines any abstract members , it should also be declared abstract itself. Mutually exclusive with final .

Extends

The keyword extends means that the new class is derived from another class, mentioned after the extends keyword. If the new class is termed a subclass, its base class is termed a super class. If the super class is an abstract class, then the new class must implement any required abstract methods for it to be instantiated.

Implements

The implements keyword means the new class will provide the required functions to implement an interface. See Interfaces for additional information.

Fields and Member Functions

Classes have fields (or members) that describe an object. Fields are defined as a list of data declarations of either fundamental and/or class type. A field data member is declared in the following manner:

  Modifier DataType  MemberName, MemberName2; 

A member function is declared in the following manner:

  Modifier Datatype  MemberName( [ParamList] ); 

The modifiers for each field are the following:

Modifier

Meaning

public

Other classes can access the field.

private

The field cannot be accessed outside of the class.

protected

The field can be accessed by the class that defines it, and its subclasses.

abstract

You re giving the signature of the method (declaring it as a member) but giving no implementation of the method. All nonabstract classes that derive from the class containing one or more abstract methods must implement all abstract methods. Does not apply to data fields.

final

For data members The field must be given an initial value in the declaration; the value can t be changed later (similar to a const in C++).

For function members The function cannot be overridden or defined in subclasses. The body of the function must be declared.

static

For data members The field does not require an object instance for access. Member is not created per instance but per class.

For function members The function can be invoked without instantiating an object. The functions have no this reference.

The following example shows a simple class with a couple of simple fields: one is the intrinsic , int , and the other is a special class, String (that is usually instantiated via new ).

 public class OurApplet 
{
public int xPosition;
private String author;
}

Data members can be initialized optionally in the same line in which they are declared. An alternative means to declare and initialize the data members in the above class might be

 public class OurApplet 
{
public int xPosition=0;
private String author="Mario";
}

Methods

Classes use methods to communicate with other objects. Methods allow you to set or get information about the current state of the object. This type of implementation encapsulates the data for the class. In the preceding example class, you would want the class to support methods to get and set the xPosition and String fields within the class.

Like classes and fields, methods also have modifiers ( public , private , and so on) that describe the scope of the method. When a method is final, it means subclasses can t override or hide the method.

Methods can return one value. This means that you can write a Get type of method that returns the xPosition, or you can return an error value. Consider the following code that implements a couple of simple methods ” getPosition and putPosition ”on the class just shown, OurApplet :

 public class OurApplet 
{
private int xPosition;
String author;
public OurApplet () { // a Constructor
xPosition = 0;
}
public void putPosition(int x) {
xPosition = x;
}
public int getPosition() {
return xPosition;
}
}

Constructors

A constructor is a function within a class that has the same name as the class and no return type, which is automatically used to initialize the object. When you call new to instantiate a new object, the constructor for that class instance is invoked. As with other functions, constructors can be overloaded.

 Public class OurPoint { 
Private int xPosition, yPosition;
OurPoint() {
yPosition = 0;
xPosition = 0;}
OurPoint( int X, int Y ) {
yPosition = X;
xPosition = Y;
}

Here s a sample:

 OurPoint P = new OurPoint( 4, 5 ); // Invoke second constructor 

Subclasses may invoke their super class constructors specifically , using the super keyword. The constructor is the only function permitted to use the super keyword. The format is

 Public class SomeClass extends SomeSuperClass { 
SomeClass( int X ) // Constructor
{
super(x); // Invoke Super class constructor, that accepts an integer.

Finalizers

Some languages implement a destructor function, which like the constructor is automatically invoked. Destructors, however, are invoked when the object goes out of scope. Java does not implement destructors, but does provide a special function called finalize , which performs a similar task.

In Java, because of garbage collection, there is no guarantee as to when an object will be released, or if it will be released at all. There are various methods to manually invoke garbage collection (such as System.gc() ), but the fact that normal execution may not invoke a finalizer is the reason that destructors are not supported.

Finalizers are used to release system resources, such as an open file or socket handle, used by a class. Finalizers can not be overridden, and they always have the same signature:

 Protected void finalize() { 
}

Static and Nonstatic Initializers

In addition to autoinitialization and constructors, classes may define a static initializer and nonstatic initializer section. These initializers are used when initialization of a data member requires more than a simple, single, line of code. They are declared like functions inside the class with { and } to denote the code section, but with no function name.

Static initializers are run when the class is loaded by the classLoader the first time. Nonstatic initializers are run when each instance of a new object is created.

 Public class Foo { 
public static int si;
public int i;

static { // Static Initializer
si = 1;
}
{ // Nonstatic Initializer
i = 2;
}

Interfaces

An interface can contain one or more member functions of constant declarations, and in some ways is similar to an abstract super class. Interfaces differ from abstract classes in the following manner:

  • An interface cannot implement any member functions, while an abstract class can.

  • A class can implement many interfaces, but have only one super class.

  • An interface is not part of any hierarchy.

  • Interfaces can be modified after being used as a super class for other interfaces, but it is strongly recommended not to.

Defining an interface requires use of the interface keyword ”for example:

  Modifier  interface InterfaceName  extends  SuperClassName 
{
// Member function signatures (bodies are not defined)
// Constant declarations
}

Interface Modifiers may only be public and/or abstract. Their definition is similar to that of the class keyword. The abstract keyword is implicit, and should not be used in newer Java programs.

An example of an interface is as follows :

 public interface Securable { 
boolean Encrypt( String Key );
boolean Decrypt( String Key );
}

If a class is created, which specifies implements Securable , then that class must provide the Encrypt and Decrypt functions to fulfill the requirements of the Securable interface.




OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net