Aqua Phoenix
     >>  Research >>  OOP  
 

Navigator
   
 
       
   

10. Using inheritance to create new objects

10.1 Summary

  • Use existing objects as superclasses; Using extends
  • Constructors
  • The bare bones of any object in a .java file

10.2 Contents

10.2.1 Use existing objects as superclasses: extends

Before we delve into creating our very own objects, we will explore using existing objects to make new objects. This concept is not necessarily designed so much for redefining the way a button looks, simply by extending it, even though this can be done as well. Typically, your objects extend very broadly defined objects, e.g. a canvas. While a button is designed to be a component that you click on, a canvas can be anything, ranging from a picture to a table to a graph, or even a button, if you design it as such. Another object that is commonly extended is a Panel. A Panel is very similar to a canvas. Initially it is empty and can be used for just about anything.

Let us use a Canvas as a superclass to make a new Object, called a redCanvas. This new object is not different from a regular Canvas, other than its red background color. In order to make our object a subclass to an already exisiting object, we will make use of extends in our class declaration:

import java.applet.*;
import java.awt.*;
import java.util.*;
import java.io.*;


public class redCanvas extends Canvas {


  public redCanvas() {

    setBackground(Color.red);
  }

}
Let's look at the line "public class" first. This line is our class declaration. Immediately following "redCanvas", we see the extends statement. In our case we extend a regular Canvas. This means that redCanvas now inherits all the functionality of a regular canvas. Disregarding the line "public redCanvas", look at the line "setBackground(Color.red)". Notice that we are calling a function or method that does not appear anywhere in our class. Then why can we simply use it and assume that it works? Since "setBackground(Color)" is defined for the object "Canvas", and since we are extending "Canvas", "redCanvas" inherits all of Canvas's functionality, including "setBackground(Color)". If you go back to the Component tool that we used a few lectures ago, you can take a look at what other methods the object "Canvas" has to offer. All of these methods can simply be used in "redCanvas".

10.2.2 Constructors

Remember that in the Java Applet framework we used a method called init() to initialize certain aspects of the applet, mostly related to layout issues. This method is called a constructor for an applet; it constructs the applets. This "constructor" exists for all other objects as well. For all other objects, a constructor is named after the class itself. Look again at the "redCanvas" object. The constructor is the method that contains "setBackground(Color.red)". Just like any other method, this constructor can take a certain set of arguments; in our case, it does not take any. It is important to note that constructors do not return any value of any type. For that reason, constructors are declared as one of the two following:

  public redCanvas() {
or

  public void redCanvas() {
For any other method, the Java compiler would most likely complain, if you do not explicitely note that the return type is void; for constructors it is not necessary because they do not return values by definition.

A very useful aspect of constructors is that you can define several different ones, each of which takes different arguments. Let's look at a red canvas that can be constructed with no arguments, and one that can be constructed with a particular size:

import java.applet.*;
import java.awt.*;
import java.util.*;
import java.io.*;


public class redCanvas extends Canvas {


  public void redCanvas() {

    setBackground(Color.red);
  }

  public void redCanvas(int width, int height) {

    setBackground(Color.red);
    setSize(width, height);
  }

}
Note that "setSize(int, int)" is yet another inherited method. Our object can now be instantiated with one of the following:

  redCanvas myRedCanvas = new redCanvas();
or:

  redCanvas myRedCanvas = new redCanvas(200, 200);
These two declarations would occur in some other object, for example in our Applet object, from where all components of an applet are usually declared. The expression "myRedCanvas" is the variable name which denotes an instance of a redCanvas. It denotes an instance, because we can have several red Canvases in our applet, e.g. myRedCanvas1, myRedCanvasBla, or hello. Variable names are totally up to the programmer. You would want your variable names to make some kind of sense though.

Even though you can have any number of constructors in an object, each of these constructors must take a different set of arguments. If you were to create two different constructors for redCanvas as follows:

import java.applet.*;
import java.awt.*;
import java.util.*;
import java.io.*;


public class redCanvas extends Canvas {


  public void redCanvas(int age, int shoesize) {

    setBackground(Color.red);
  }

  public void redCanvas(int width, int height) {

    setBackground(Color.red);
    setSize(width, height);
  }

}
then even though you meant to create two different constructors, they both take the same order of the same types or arguments. The way the compiler looks at it, you have just created two constructors of type "redCanvas(int, int)". Should this be valid, and you would now create a redCanvas, how would Java know which constructor to use? Well it wouldn't, which is why the above example is invalid.

Let's say that you have a complicated object that needs a lot of construction, and in addition you have decided to create several constructors that take different types of arguments. Instead of writing out each constructor separately and copying and pasting the instantiating statements, you can also call another constructor from within a constructor. Let's look at an example:

import java.applet.*;
import java.awt.*;
import java.util.*;
import java.io.*;


public class redCanvas extends Canvas {


  public void redCanvas(int width, int height) {

    setSize(width, height);
    setBackground(Color.red);
  }

  public void redCanvas() {

    this(100, 100);
  }

}
The first constructor should be straight forward, given our discussion above. Let's look at the second constructor. This one is called without arguments; yet you would like to instantiate a red Canvas with some size; after all, everything has some size. But since the instantiating class has not passed a size, you decide to give the red canvas a default size of (100, 100). Instead of writing out the appropriate functions yourself, you simply call one of the other constructors with the default size as arguments. this simply refers to this very class, i.e. redCanvas. The keyword this is a Java inherent feature, and can be used for just about any object.

If you do not wish to allow a red Canvas to be created without the size arguments, then you would simply not have a constructor with no arguments.

10.2.3 The bare bones of any object in a .java file

Let NAME_OF_CLASS denote the name of the class. The the following is the bare bone framework of Java Objects, as we will be using them:

import java.applet.*;
import java.awt.*;
import java.util.*;
import java.io.*;


public class NAME_OF_CLASS extends SOME_SUPERCLASS {


  public void NAME_OF_CLASS() {

  }

}
"extends SOME_SUPERCLASS" is only then necessary, when you extend some other class. We will soon learn about making our very own objects without extending any other objects.