Lecture 11
Lecture 11
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:
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Review of Casting
In IPRG001, you used casting on primitive data types
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
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?
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
3
Types Of Inheritance
implements)
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Overriding
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:
4
A Group 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
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
for(initialisation;exit condition;increment)
for (element:collection)
• Colon separation
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 "));
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
What is Polymorphism?
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()
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?
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Model - Data
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
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
The goal of the MVC pattern is to separate the Model and View
by using a Controller
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
8
Observer Pattern
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
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Simple Window
It only has a title “This is my window”
9
Simple Example
Class SimpleWindow
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Observer Steps
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