The look of a class

As we have already heard, an object-oriented system consists of a hierarchy of classes with one root class at its top.

 

The root class in the Java API is the Object class, located in the java.lang package. It is a class with very general behaviour and all other classes inherit and extend this behaviour.

 

Now it will be shown, what a class looks like, by defining a simple class:

A simple class
A simple class

This simple class has a "header" that consists of the visibility (indicates, from where this class may be instantiated), the word class and a name for this class.

 

The "body" of this class only has a constructor that is used, when this class is instantiated. Each statement inside the constructor will be executed and usually contains statements that initialize variables or call methods.

 

The statement super() (always called implicitely) simply calls the constructor of this class' superclass. Because the Simple class is not explicitely derived from any other class by adding extends [name of superclass], Java automatically declares the Object class to be the superclass.

 

If you are a C++ programmer, you are probably looking for a destructor. You won't find one, because, as mentioned in the previous section, Java automatically handles the dereferencing of objects with its garbage collection.

 

For this simple class is pretty useless, let's extend it to a not that simple class:

A not that simple class
A not that simple class

As you can see, this class is derived from the Simple class by adding extends Simple after the class name. Thus, our new class inherits all the functionality and behaviour of the Simple class, as well as of the Object class.

 

This class now has one class variable (or member variable) value and a method getValue(), which returns the variables' value.

 

The constructor has now something more to do: First, the superclass constructor is called by super() (which calls its own superclass constructor; that's inheritance!) and then assigns the parameter value to the class variable value. For the names of the parameter and the class variable are the same, the prefix this. must be added to the variable's name to indicate that it belongs to a variable of this class.

 

The method getValue() is also given a visibility, which indicates from where this method may be invoked. Giving the method a public visibility means that this method may be called from any other class.

 

The possible visibilities of a method, regarding to same class, subclass, package, and world are:

 

public: The method may be called from anywhere.

protected: The method may be called from anywhere but from the world.

friendly: The method may be called from inside the same class, as well as from the same package.

private: The method may be called only from inside the same class.

 

Now we want to use this class and thus have to create an object.

Neural Net Components in an Object Oriented Class Structure