Defining Classes
Home Up Search Java 2 API C++ Resources

In Java, everything is an object and a program is a collection of interacting objects.   Thus the code for a Java program consists of a collection of class definitions.  

To define a class a number of things must be specified:

  1. The name of the class.   The name of the class should start with a capital letter and match the name of the file in which it is defined.
  2. The visibility of the class.  
  3. The class from which this class inherits (its "superclass").
  4. Any interfaces that this class implements.
  5. Any properties/attributes of the class.    These are also called "instance variables".
  6. The visibility of those variables.   Normally the instance variables would be either private or protected.  
  7. Any methods of the class, including the number and types of their arguements.   This is called the "signature" of the method.
  8. The visibility of the methods.

The syntax to declare a class is:

[scope] [qualifier] class class_name extends superclass_name implements interface_name1 [, interface_name2]
{

    [scope] [qualifier] type variable_name1 [= initial_value];

    [scope] [qualifier] type variable_name2 [= initial_value];

    [scope] class_name([type arg1] [, type arg2] ...);

    [scope] class_name([type arg1] [, type arg2] ...);

    scope [qualifier] return_type method_name1 ( [ type arg1] [, type arg2] [...])
    {
       [body of method]
     }

    scope [qualifier] return_type method_name2 ( [ type arg1] [, type arg2] [...])
    {
       [body of method]
     }

}

Where

  • "class" is a keyword denoting the beginning of a class definition.   Note that in Java, the name of the file containing a class definition has to be the same as the class name.   Thus there is only one class definition per file.
  • "scope" is the visibility of the class, property, or method.   If blank, package visibility will be assumed.  See the page on scoping for more information.
  • "qualifier" is either blank, "static" or "abstract".   "Static" means that the the variable or method is common to all instantiations and thus can be accessed even if no instance of the class is present.   "Abstract" means that the class or method represent an abstract concept and not any particular enitity.  Thus, no instance of that class can be made.   Only instances of a subclass can be created.  See the page on polymorphism.   Abstract methods have no body--see the page on methods.
  • "class_name" is the name of the class being defined.   It should start with a capital letter.
  • "extends" is a keyword denoting [class_name] is a subclass (child class) of the [superclass_name] (parent class).   See the page in inheritance.   A class can be only subclassed from one parent in Java (no multiple inheritance).  "Extends" can be omitted if the class is extending the root class, Object.
  • "implements" is a keyword denoting that [class_name] is implements the methods defined in the interface [interface_name].  Note that it is possible to implement several different interfaces simultaneously.
  • See the page on declaring variables for information on the syntax of instance variables (properties).
  • See the page on using methods for information on how to declare and use methods.
  • Constructors are special static methods that have the same name as the class.   See the page on constructors for information on how to write them.    Declaring a constructor is optional and there may be more than one, so long as they have different signatures.

Example:

An abstract class:

public abstract class Human 
{

   private Color hairColor;

   private String name;


   public abstract Human(Color hairColor, String name)
   {
      this.hairColor= hairColor;
      this.name = name;
   }

   public Color getHairColor()
   {

      return hairColor;
   }

   public String getName()
   {
       return name;

   }

   public abstract String sayGreeting();
}

A concrete class:

public class FrenchPerson extends Human
{

    public String sayGreeting()
    {

         return "Bonjour!";
    }

}