Review of Java and Object Oriented Programming
1.1 Overview of Java programming
A Brief History of Java
Java is a high level language.
Java is an object-oriented language developed by Sun Microsystems.
Many of the concepts and syntax of Java are borrowed from C++.
Can be used to create two types of programs: applications and applets.
Robust: no pointers in java and no manual memory handling.
The Sun development team soon realized that the Java language would be suitable for
writing programs to run on different computers connected to the Internet.
Java programs are portable.
Java promise: “Write once, run everywhere”.
Java differs from other programming languages in that it is both compiled and interpreted
Java compilers produce the Java bytecode.
Java bytecodes are a set of instructions written for a hypothetical computer, known as the
Java virtual machine.
Bytecodes are platform-independent.
The JVM is an interpreter for bytecode.
An interpreter reads the byte code and translates into a sequence of commands that can be
directly executed by the computer.
Because the execution of every Java program is under the control of the JVM, the JVM can
contain the program and prevent it from generating side effects outside of the system.
Object Oriented Programming
In the older styles of programming, a programmer who is faced with some problem must identify
a computing task that needs to be performed in order to solve the problem. Programming then
consists of finding a sequence of instructions that will accomplish that task. But at the heart
of object-oriented programming, instead of tasks we find objects– entities that have behaviors,
that hold information, and that can interact with one another. Programming consists of
designing a set of objects that model the problem at hand.
All computer programs consist of two elements: code and data.
In OO programming, a system or problem is decomposed into entities called objects.
OOP instead of focusing on what the system has to do, focus on:
o What objects the system contains
o How they interact
In OO, data is critical – data is not allowed to move freely around a system, but it is tied
closely to the functions that operate on it.
An object has data members and functions (methods in java).
Data is protected because the data belonging to an object can only be accessed and
modified by the methods of that object.
Different objects can 'talk' to each other through their methods. In this way, Object A
cannot directly modify data belonging to Object B – but it can call a method of Object
B that in turn modifies the data.
To summarize the OO approach:
o Emphasis on data, not procedure
o Programs are made up of objects
o Data is hidden from external functions
o Objects can communicate with each other through methods
The basic concepts of OO are:
o Objects and classes
o Data abstraction and encapsulation
o Dynamic (late) binding
o Message communication
These are explained below.
o Objects and Classes
Represent ‘things/objects’ from the real world, or from some problem
domain (example: “the red car down there in the car park”)
A set of data items (fields) with operations (methods - that implement
class behaviors) to manipulate them
Has characteristic behavior.
Combines data and operations in one place
An object is also known as an instance. E.g Ibsa is an instance of a Student
Defines a type of object (example: “car”)
Specifies methods and data that type of object has
Map of a building(class) vs. building (object)
Ignore details when appropriate
o Think about what a method/object/class does, not how it does it
o Treat it as a black box
Is the mechanism that binds together code and the data it manipulates
Put related things in the same place
o I.e. group related data and operations in an object
o Each object has its own data and knows how to use it
Hide internal representation/implementation
o Object = black box
o Deny external access to internal fields/methods
o Also called information hiding
Access an object through its interface:
o Set of externally accessible fields and methods
o Should not change even if internal implementation does
Goal of encapsulation: to transparently use different implementations of same object
o When you drive a car, you don’t have know the details of how many cylinders the
engine has or how the gasoline and air are mixed and ignited.
o Only have to know how to use the controls.
Inheritance is the process by which one object acquires the properties of another object.
Allows reuse of implementation
Classical Inheritance: “is-a” relationship
Example: fruits and types of fruit (an apple is a type of fruit)
Goal of inheritance: code reuse
The ability to take more than one form
Different objects can respond differently to the same message
In terms of the OOP, this means that a particular operation may behave differently for
different sub-classes of the same class.
E.g Shape (circle, rectangle, etc) – draw or calculate area
AWT and Swing
Within this chapter you will be introduced to many of the Java components for constructing
graphical user interfaces (GUIs). With the information in this chapter you will be able to
develop your own GUIs.
A graphical user interface (GUI) presents a user-friendly mechanism for interacting with an
application. A GUI gives an application a distinctive “look” and “feel.” There are two main sets
of visual components and containers for user interface design in JAVA:
`–AWT (Abstract Window Toolkit) and
2.2Concepts of AWT and Swing
Before Swing was introduced in Java SE 1.2, Java GUIs were built with components from the
Abstract Window Toolkit in package java.awt. Package javax.swing contains swing components
that can be used to build Java GUIs. Most Swing components are pure Java components—they
are written, manipulated and displayed completely in Java.
2.2.1 An Overview of AWT
The AWT supports GUI Java programming. The AWT provides the connection between your
application and the native GUI. The AWT provides a high level of abstraction for your Java
program since it hides you from the underlying details of the GUI your program will be running
on. AWT is fine for developing simple graphical user interfaces, but not for developing
comprehensive GUI projects.
AWT features include:
A rich set of user interface components.
A robust event-handling model.
Graphics and imaging tools, including shape, color, and font classes.
Layout managers, for flexible window layouts that don't depend on a particular window
size or screen resolution.
Data transfer classes, for cut-and-paste through the native platform clipboard.
The AWT components depend on native code counterparts (called peers) to handle their
functionality. Thus, these components are often called "heavyweight" components.
2.2.2 An Overview of Swing
Swing implements a set of GUI components that build on AWT technology and provide a
pluggable look and feel. Swing is implemented entirely in the Java programming language, and
is based on the JDK 1.1 Lightweight UI Framework.
Swing features include:
All the features of AWT.
100% Pure Java certified versions of the existing AWT component set (Button, Scrollbar,
A rich set of higher-level components (such as tree view, list box, and tabbed panes).
Pure Java design, no reliance on peers.
Pluggable Look and Feel.
Swing components depend less on the target platform and use less of the native GUI resource.
For this reason, Swing components that don’t rely on native GUI are referred to as lightweight
components, and AWT components are referred to as heavyweight components.
2.2.3 AWT vs. Swing
There are, of course, both pros and cons to using either set of components from the JFC in your
Java applications. Here is a summary:
Speed: use of native peers speeds component performance.
Applet Portability: most Web browsers support AWT classes so AWT applets can run
without the Java plugin.
Look and Feel: AWT components more closely reflect the look and feel of the OS they
Portability: use of native peers creates platform specific limitations. Some components
may not function at all on some platforms.
Features: AWT components do not support features like icons and tool-tips.
Portability: Pure Java design provides for fewer platform specific limitations.
Behavior: Pure Java design allows for a greater range of behavior for Swing components
since they are not limited by the native peers that AWT uses.
Features: Swing supports a wider range of features like icons and pop-up tool-tips for
Look and Feel: The pluggable look and feel lets you design a single set of GUI
components that can automatically have the look and feel of any OS platform (Microsoft
Windows, Solaris, Macintosh, etc.). It also makes it easier to make global changes to
your Java programs that provide greater accessibility (like picking a hi-contrast color
scheme or changing all the fonts in all dialogs, etc.).
Applet Portability: Most Web browsers do not include the Swing classes, so the Java
plugin must be used.
Performance: Swing components are generally slower than AWT. Since Swing
components handle their own painting (rather than using native API's like DirectX on
Windows) you may run into graphical glitches.
Look and Feel: Even when Swing components are set to use the look and feel of the OS
they are run on, they may not look like their native counterparts.
2.2.4 Lightweight vs. Heavyweight GUI Components
Most Swing components are not tied to actual GUI components supported by the underlying
platform on which an application executes. Such GUI components are known as lightweight
components. AWT components (many of which parallel the Swing components) are tied to the
local platform and are called heavyweight components, because they rely on the local
platform’s windowing system to determine their functionality and their look-and-feel.
2.3The Java GUI API
The GUI API contains classes that can be classified into three groups: component classes,
container classes, and helper classes. Their hierarchical relationships are shown in Figure 1.
Fig 1: Java GUI programming utilizes the classes shown in this hierarchical diagram.
The component classes, such as JButton, JLabel, and JTextField, are for creating the user
interface. The container classes, such as JFrame, JPanel, and JApplet, are used to contain other
components. The helper classes, such as Graphics, Color, Font, FontMetrics, and Dimension,
are used to support GUI components.
A. Component Classes
An instance of Component can be displayed on the screen. Component is the root class of all
the user-interface classes including container classes, and JComponent is the root class of all the
lightweight Swing components.
B. Container Classes
An instance of Container can hold instances of Component. Container classes are GUI
components that are used to contain other GUI components. Window, Panel, Applet, Frame,
and Dialog are the container classes for AWT components. To work with Swing components,
use Container, JFrame, JDialog, JApplet, and JPanel, as described in Table 1.1.
TABLE 1.1 GUI Container Classes
Container Class Description
java.awt.Container is used to group components. Frames, panels, and applets are its subclasses.
javax.swing.JFrame is a window not contained inside another window. It is used to hold other Swing user-interface
components in Java GUI applications.
javax.swing.JPanel is an invisible container that holds user-interface components. Panels can be nested. You can
place panels inside a container that includes a panel. JPanel is also often used as a canvas to
javax.swing.JApplet is a subclass of Applet. You must extend JApplet to create a Swing-based Java applet.
javax.swing.JDialog is a popup window or message box generally used as a temporary window to receive additional
information from the user or to provide notification that an event has occurred.
C. GUI Helper Classes
The helper classes, such as Graphics, Color, Font, FontMetrics, Dimension, and
LayoutManager, are not subclasses of Component. They are used to describe the properties of
GUI components, such as graphics context, colors, fonts, and dimension, as described in Table
TABLE 1.2 GUI Helper Classes
Helper Class Description
java.awt.Graphics is an abstract class that provides the methods for drawing strings, lines, and simple shapes.
java.awt.Color deals with the colors of GUI components. For example, you can specify background or
foreground colors in components like JFrame and JPanel, or you can specify colors of lines,
shapes, and strings in drawings.
java.awt.Font specifies fonts for the text and drawings on GUI components. For example, you can specify
the font type (e.g., SansSerif), style (e.g., bold), and size (e.g., 24 points) for the text on a
java.awt.FontMetrics is an abstract class used to get the properties of the fonts.
java.awt.Dimension encapsulates the width and height of a component (in integer precision) in a single object.
java.awt.LayoutManager specifies how components are arranged in a container.
2.4 Event Handling
Event and Event Sources
When you run a Java GUI program, the program interacts with the user, and the events drive its
execution. An event can be defined as a signal to the program that something has happened. The
code that performs a task in response to an event is called an event handler and the overall
process of responding to events is known as event handling. Events are triggered either by
external user actions, such as mouse movements, button clicks, and keystrokes, or by internal
program activities, such as a timer. The program can choose to respond to or ignore an event.
The component that creates an event and fires it is called the source object or source component.
For example, a button is the source object for a button-clicking action event. An event is an
instance of an event class. The root class of the event classes is java.util.EventObject.
Table 3 lists external user actions, source objects, and event types fired.
Table 3: User Action, Source Object, and Event Type
User Action Source Object Event Type Fired
Click a button JButton ActionEvent
Press return on a text field JTextField ActionEvent
Select a new item JComboBox ItemEvent, ActionEvent0
Select item(s) JList ListSelectionEvent
Click a check box JCheckBox ItemEvent, ActionEvent
Click a radio button JRadioButton ItemEvent, ActionEvent
Select a menu item JMenuItem ActionEvent
Move the scroll bar JScrollBar AdjustmentEvent
Move the scroll bar JSlider ChangeEvent
Window opened, closed, iconified,
deiconified, or closing
Mouse pressed, released, clicked,
entered, or exited
Mouse moved or dragged Component MouseEvent
Key released or pressed Component KeyEvent
Component added or removed from
Component moved, resized,
hidden, or shown
Component gained or lost focus Component FocusEvent
Listeners, Registrations, and Handling Events
Java uses a delegation-based model for event handling: a source object fires an event, and an
object interested in the event handles it. The latter object is called a listener.