Contents Index A Java seminar Subclasses in Java

ASA Programming Guide
  Introduction to Java in the Database
    A Java seminar

Understanding Java classes


A Java class combines data and functionality—the ability to hold information and perform computational operations. One way of understanding the concept of a class is to view it as an entity, an abstract representation of a thing.

You could design an Invoice class, for example, to mimic paper invoices, such as those used every day in business operations. Just as a paper invoice contains certain information (line-item details, who is being invoiced, the date, payment amount, payment due-date), so also does an instance of an Invoice class. Classes hold information in fields.

In addition to describing data, a class can make calculations and perform logical operations. For example, the Invoice class could calculate the tax on a list of line items for every Invoice object, and add it to the sub total to produce a final total, without any user intervention. Such a class could also ensure all essential pieces of information are present in the Invoice and even indicate when payment is over due or partially paid. Calculations and other logical operations are carried out by the methods of the class.

Example 

The following Java code declares a class called Invoice. This class declaration would be stored in a file named Invoice.java, and then compiled into a Java class using a Java compiler.

Compiling Java classes 
Compiling the source for a Java class creates a new file with the same name as the source file, but with a different extension. Compiling Invoice.java creates a file called Invoice.class which could be used in a Java application and executed by a Java VM.

The Sun JDK tool for compiling class declarations is javac.exe.

public class Invoice {
  // So far, this class does nothing and knows nothing
}

The class keyword is used, followed by the name of the class. There is an opening and closing brace: everything declared between the braces, such as fields and methods, becomes part of the class.

In fact, no Java code exists outside class declarations. Even the Java procedure that a Java interpreter runs automatically to create and manage other objects—the main method that is often the start of your application—is itself located within a class declaration.


Subclasses in Java

Contents Index A Java seminar Subclasses in Java