# Java: Example - Tiny Window with subclass

This is a reimplementation of the TinyWindow program, but using the most common style of building windows by defining a subclass of JFrame. All the work of buiding the JFrame will be put into its constructor, as will be illustrated more in later examples.

How many classes? This has been divided into two classes, one containing the main program and one containing the JFrame subclass. It's perhaps more common to just stick the main program into one of the other classes, for example the subclass of JFrame, but that confuses the issues somewhat. For clarity I've separated them.

How many files? Each class can, and probably should be, put in a separate file. Some future examples will use separate files.

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
    // TinyWinSubclass.java - Common JFrame subclass style for GUI
    // Fred Swartz, 2004-10-28

    import java.awt.*;
    import javax.swing.*;

    ////////////////////////////////////////////////// class TinyWinSubclass
    class TinyWinSubclass {
        //====================================================== method main
        public static void main(String[] args) {
            JFrame window = new TinyWinGUI();                      //Note 1
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setVisible(true);
        }
    }

    /////////////////////////////////////////////////////// class TinyWinGUI
    class TinyWinGUI extends JFrame {                              //Note 2
        //====================================================== constructor
        public TinyWinGUI() {                                      //Note 3
            //... Set window characteristics
            this.setTitle("Tiny Window using JFrame Subclass");    //Note 4
        }
    }

## Notes

Note 1: Create a new object (instance) of our own window class.
Note 2: This new class is a subclass of JFrame (extends JFrame). This means it can do everything a JFrame can, in addition to what we define in it.
Note 3: A "constructor" is like a method to tell how to initialize a class. You can tell the difference between a constructor and a normal method by two characteristics: (1) It has the same name as the class, and (2) It has not return type.
Note 4: Because TinyWinGUI is a subclass of JFrame, we're calling methods in "ourself". We can simple write the "setTitle" call, or as here, we can put "this." in front of it to clarify that this method is in our class or one of our anscestors.