![]() |
|
![]() |
Today we will find out how to do some file handling and present the result in a window, a graphical user interface
well-known to you. I presume you have installed Swing, since
every graphical example from now on will make use of Swing. But first we will start looking into the more useful
parts of Object-Oriented Programming (OOP), a topic that continues a few issues since it is such comprehensive.
Object-Oriented Programming
Let us recall that a class is the blue print out of which any object is instantiated. A class can have
many class variables holding values and states, and many methods doing the workload. A primary
object is to make a class as clean as possible, that is, do not merge to many functions into it. Let a class be
as pure as possible, such as a screw-driver, as a knife, or as scissors, not as a Swiss army knife. On the contrary
a Swiss army knife is built out of several classes, but is itself only a container of the other classes. Got the
idea? |
|
In Java anything
static is a class variable or method that exist though no object needs to be instantiated
from that class. Any static variable or method might be used at any time, they stick around but do not belong
to any particular object but to the class. |
System
class.Take your time and look up your Java API (preferably the framed version of 1.2). Chose Package :
lava.lang
and then System.
In the class description you will not find any method named
println()
, but a variable out
. First you may notice that this variable is static
,
and second you see it is a holder of another class, PrintStream
. This class, in turn, holds the
println()
as is seen under the "See also" block.
Hence, you use the class System, that in turn uses the class PrintStream
.
Whenever a class, or a method, is static
we do not have to instantiate new objects from classes.
But recall, we made ourselves a MyBank class that in turn made a number of BankAccount objects, named
john, chris,
etcetera. The class was instantiated and then we could access the class methods from the objects.
Since we did not make these methods static
they doesn't exist if no object is made.
|
Still we use the class BankAccount from the driver class MyBank. To use is to manage
and/or manipulate other classes or objects.
Another example is the Swiss army knife that contains other objects.
The distinction between use and contain is somewhat fuzzy since many times an object of a class may both use
and contain objects of other classes.
![]() An illustration of inheritance |
Think of our BankAccount as an example. It is quite basic, don't you think? For example, there is no interest.
Let us create such a class then.
Then we start sketch a class with exactly the same methods and variables, but we see that we only added two
methods to compute the interest and set the rate of interest, further one variable holding the rate of interest.
Here inheritance comes to play.
So, design a class that only has these new features but first states that "I am a BankAccount, but I can
do this stuff too."
Inheritance is simply to add more specified properties to an old class, as the illustration shows. We have
the BankAccount class (from Into Java 2) and we add a new class that will have every variable and method from
that one (since we made them protected
and not private
), but adds one data field and
two methods.
How do we do that in real life? We simply make a new class as we are used to, but tell the compiler that we
would like to make this class an extension to another class:
|
Obviously the magic word is extends
that lets you inherit from one super class. (See
no bad in that, please, it really makes the code less error prone.) Now we have class that is-a
sub class of a super class, and the is-a phrase is the essence of inheritance.
Now it is no pain to simply add the variable and the methods:
|
And we are done. Interesting points are that we now can choose whether we like to use the basic account or
the account with an interest as we like to. Further, both of them are treated as BankAccounts by Java if you do
not explicitly set a new reference variable to InterestAccount, but why should you?
Operators
Java supports the operators in a mathematical correct way, that is: |
All these three relationships between classes--use, "has-a" and "is-a"--is extensively
used in any OOP language, Java is no exception.
In fact, Java has so many classes and features that it is almost impossible to get to know them all at start,
and you really don't have to know the exact inner workings of everything.
Taking in information from the keyboard is no difference to that, and last time we made use of System.in,
InputStreamReader
and BufferedReader
. Everyone of them is extensively used by developers,
and I suggest you surf around the Java API to find some more information on them, since I will not dig deeper
than this for a long time to be. Anyway, you can see how the latter makes use of and contains
the intermediate, that uses the first one.
Today I will make use of File
and FileReader,
as well as BufferedReader
.
File
is a class in the java.io
package that is most convenient to use whenever handling
files and file names. Yes, most readers can be instantiated from the string representation of the file
name, but having File
you never need worry, and there are several useful methods and variables at
your service. File
as an argument and provide a body to
more convenient classes, as BufferedReader
. Using such classes as FileReader
is a great
help to beginners, but when time passes you will explore other classes as well. JFrame
(most classes
in Swing have counterparts in the older AWT, the difference is in the "J
"). A JFrame
is (almost) nothing more than an empty window with the upper title bar with system dependent buttons.Hence it is convenient to add one little feature to this otherwise empty class of ours, a way to close the
window with the cross faced button, Alt+4 and Close from the upper left system menu. Therefore we inherit from
JFrame
and add that feature. This is the result:
|
So far we can view the window by this little driver class:
|
And, yes, it worked!
At this point most people used to the old styled sequential programming raises one's eyebrows. In spite of
the main method really exiting since it is done, the window do not disappear. That is because now the more powerful
features of Java are in action. But click the cross once and it is disposed.
JFrame
is in fact a sub sub sub class to Container,
a class with a descriptive name.
From that we understand that we have to fill this frame with other stuff, that we first have to implement, so
off we go.
getContentPane()
JFrame
is not that simple as one might think. Normally developers use some
kind of pane to add other objects into. JFrame
is no exception to that, but there is actually only
one thing that is of interest to us, the content pane. The content pane is the place to add stuff into,
it is a "has-a" object, we really don't use it but stuff it with valuable objects.Our goal is to create a window with one big text area showing the contents of the files you give to the program.
Since we add no intelligence to this little app it can only show text files, but that will do. Let us add some
new lines to MyFirstFrame.java
.
|
You can try it with the driver used a moment ago, and you'll see a window with the text we set at initialization.
Since we did not add any way to specify a file name we never invoked readFile()
this time. Then let
us add that feature, we can make it interactive through the terminal window.
Last time we used a while loop to get user input and this time we can copy that idea. Hence add to MyFirstFrameDriver.java
a few lines.
|
With this little effort we have now made ourselves a graphical app that may show any text file of our wish.
That is possible since OOP gives us the strength of using, having and inherit any class. And since Java have so
many prebuilt classes of such quality.
Further we have discussed the three relationships that make OOP that strong, using other classes,
containing other classes and inheriting other classes. I hope I have wet your
appetite that much you will continue following this column.
As an extra homework, please surf through the Java 1.2 API and look up the classes we have used, the methods
available, even though we haven't used them today. Have a nice time.
![]() |
|
![]() |
|
Copyright © 2000 - Falcon Networking |
ISSN 1203-5696 |
Feb 2000 |