Information Technology
Applications Programming
LECTURE 11
Subject Review
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Subject Review
Learning Objectives
At the end of the lecture, you should be able to:
Demonstrate a working knowledge of the advanced
constructs in the object-oriented language Java.
Describe elements that constitute a good OO design.
Design and develop a well-designed system that correctly
implements a solution to a problem defined in a specification.
Describe and correctly use inheritance in Java.
Design and develop a GUI interface.
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Review of Casting
In IPRG001, you used casting on primitive data types
• int result = (int)(height*length);
• This uses height (as a double)
Simple Cast then casts the multiplication
result to an int
• int result = ((int)height)*length
Immediate • This casts height to an int before
it is used in multiplication
Cast
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
1
Basic Concepts
Abstraction
Abstraction is the reduction of information back to a basic
concept.
We remove characteristics to reduce the object down to
essential characteristics only
A class is an abstraction of an object
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Basic Concept
Encapsulation
Encapsulation is the grouping of data and behaviour together
into distinct bundles
Data and behaviour is captured and held (encapsulated) by
an object.
Attribute
Values
Methods
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Overloading
A method can be overloaded
Java matches a method call to a method definition with the
– name
– arguments: number, type, and order
This is called the signature of the method.
More than one method can have the same name in a class or
package
Method signature can not be the same ie cannot have the exact
same arguments
Example:
sleep()
sleep(int time)
sleep(boolean stop)
sleep(int start, int time)
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
2
Abstract Class
Abstract Class
• May contain abstract methods (method
declarations without implementation).
• Cannot be instantiated but it can be sub-classed.
• May contain some implemented methods.
• May contain changeable attributes
If a class contains an abstract method the class must be declared
abstract
If an abstract class only contains abstract methods it should be
declared as an interface
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Interface
Interface
• is a special type of class-like structure
• only contains constant declarations and method signatures.
• Cannot be instantiated (ie no objects)
• Can only be implemented by another class or extended by another
interface
• An interface contains only abstract methods.
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
What is Inheritance?
Inheritance is the passing on of characteristics or attributes
from one entity to another
A child inherits certain characteristics e.g. eye colour, from a
parent
Now lets look at Class Inheritance….
Remember: A class has a set of characteristics
• methods
• attributes
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
3
Types Of Inheritance
• Inherits all methods & attributes from the
Implementation parent class
Inheritance • class A extends B
• There can only be one parent class
(keyword
extends)
• Inherits from an interface (skeleton – empty
Interface methods & constant attributes only)
Inheritance • class A implements InterfaceB
• A class can inherit from many interfaces
(keyword • Example: Class A extends B implements C, D
implements)
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Overriding
A child may override a parent method
– The child class can provide an alternate implementation
for a parent’s method
– Method signature must be the same
– A child must override an abstract method in the parent
class
– A parent method cannot be overridden if it is declared
final
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Enum
Is an enumerated data type, that is, you can declare a list of
constants that the runtime will treat as numbers.
Example:
public enum Direction { NORTH, SOUTH, EAST, WEST }
enum inherits from the Enum class in the API, so you
cannot inherit from any other class.
An enum class has a private constructor, that cannot be
invoked, the runtime uses it to create the constants.
enum is a type of singleton (only allows 1 instance at any
time)
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
4
A Group Class
A common pattern is to store the list in its own class
Class Objects
{
private LinkedList<Object> objects = new
LinkedList<Object>();
public void method()
{
for (Object object: objects)
object.method();
}
}
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
List Lookup Pattern
A common pattern is to give each object an id. To find an object
in the list, we can scan each object until we find one that
matches.
The supplier offers a boolean function
public boolean matches(int id)
{ return this.id == id; }
The client scans the list and returns the matching object
private Object find(int id)
{ for (Object object: objects)
if (object.matches(id))
return object;
return null; }
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Enhanced for loop
In IPRG001, you used for loops like this
for(initialisation;exit condition;increment)
• Semi colon separation
Java also allows for-each loops like this
for (element:collection)
• Colon separation
For-each loops should be used where possible
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
5
For-each LinkedList Example
LinkedList<Customer> customers =
new LinkedList<Customer>();
customers.add(new Customer(" Peter "));
customers.add(new Customer(" Bill "));
for (Customer customer : customers)
{
System.out.println("Customer is: " + customer.getName());
}
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
What is Polymorphism?
Different objects can react differently when the same method is
called.
Polymorphism means many forms. In OO, it means that a
characteristic can have a different meaning or behaviour in a
different context.
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Polymorphism
Objects are treated the same, the results are different because
each sub class has a different implementation of area() &
perimeter()
A list of shape objects, iterate through the list, call
area() and perimeter() for each shape
– Lines 151 – 152
System.out.println("Area is:" + current.area());
System.out.println("Perimeter is:" + current.perimeter());
The power of Polymorphism, Encapsulation and Inheritance
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
6
Dynamic Dispatch
How did the program know to use the Rectangle, Circle and
Triangle implementations (respectively) when it called area()
and perimeter() on each element of the List?
In the polymorphism example
– LinkedList was declared as type Shape – line 133
private LinkedList<Shape> shapes = new LinkedList<Shape>();
– current was declared as type Shape – line 149
for(Shape current : shapes)
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Non- Polymorphism – Unique Features
If a child class has a unique feature not define by the parent, it
can only be accessed using an if or case statement.
This is ugly and should be avoided where possible. It can be
confusing for other developers to understand and hard to
maintain.
But …. here is an example for the child method diameter() for
the Circle class
See Unique Code example.doc
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
MVC – Model View Controller
The Model View Controller architecture is used for most
(modern) GUI systems.
It separates an application into 3 layers:
Model - Data
View – Display elements
Controller - Mechanism for adding new data and
controlling what data is displayed
This allows:
– The GUI to be separate from the data
– The control mechanism to be separate from the GUI
– Multiple views to be updated separately
– Low coupling & high cohesion
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
7
MVC – Model View Controller
The Model View Controller architecture is used for most
(modern) GUI systems.
It separates an application into 3 layers:
Model - Data
View – Display elements
Controller - Mechanism for adding new data and
controlling what data is displayed
This allows:
– The GUI to be separate from the data
– The control mechanism to be separate from the GUI
– Multiple views to be updated separately
– Low coupling & high cohesion
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
MVC Summary
Implement the Model; implement the View; Create the
Controller; Assign the Controller to the View
Implement the Model
Implement the View
– Decide which GUI components to use eg buttons etc
– Decide how to arrange the GUI components
– Decide which layout manager(s) to use
Create a Controller (action listener)
• Define the actionPerformed() method ie what must happen
when the event occurs
• Use the ActionEvent object to determine information about
the event
Assign the Controller to the View, that is, assign the
action listener to the required GUI component(s)
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Problems with MVC
The goal of the MVC pattern is to separate the Model and View
by using a Controller
But as we have just seen in the MVC diagram there is still a
relationship between View and Model
So if we add another View, the Model needs to be updated to
handle it.
This is not what we want. We need to decouple View and
Model
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
8
Observer Pattern
The Observer Pattern is a design pattern
Model knows about the Observer not the View
Controller
<Interface>
Model Observer
View
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
GUI Overview
Windows inherit from JFrame
Panels inherit from JPanel
Buttons are instances of JButton
Textboxes are instances of JTextField
Labels are instances of JLabel
For Swing Components
import javax.swing.*;
public class MyWindow extends JFrame
public class MyPanel extends JPanel
private JButton button= new JButton("Calculate");
private JTextField field = new JTextField(10);
private JLabel label = new JLabel(“Text to display”);
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Simple Window
It only has a title “This is my window”
public class SimpleWindow extends JFrame
{
private final int WIDTH = 350, HEIGHT = 250;
public SimpleWindow()
{
super("This is my window");
setup();
setVisible(true);
}
private void setup()
{
setSize(WIDTH, HEIGHT);
setLocation(200, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
9
Simple Example
Class SimpleWindow
Here we have a window with a title
The width and height of the window are set using setSize()
setLocation() determines where the window will be drawn
setDefaultCloseOperation() determines what will happen
when the window is setVisible() determines if the window is
drawn on screen
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Observer Steps
4 steps to implement the Observer pattern
All Models inherit from Updater
All procedures in models call updateViews()
All panels inherit from MyObserver
All panels attach() to model(s)
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Key Concepts
Summary (write your own)
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
10