Information Technology
Applications Programming
LECTURE 1
Review of Object Oriented Concepts
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Review of Object Oriented Concepts
Learning Objectives
At the end of the lecture, you should be able to:
Describe the differences between an Object and a Class
Identify and use default and alternate constructors
Identify and use procedures and methods
Describe the 3 types of reuse in Object Oriented
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Basic Concepts
Object
An object is a building block of object oriented
An object can be tangible thing in the real world eg a pen or a
concept eg a bank account
It has properties and behaviour eg a student has a name (property)
and can study (behaviour)
An object is an instance of a class
Class
A class is like a template – a framework
A class defines the properties and behaviours of its objects
A Student class describes all of the properties and behaviours
of all instances (objects) of the class, that is; a student object
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
Naming a Class
Always starts with Capital Letter, compound name is
A class always starts with a Capitial Letter, example, House,
Student, Desk
If a class has a compound name then it is letter cased,
example, BrickHouse, ComputerDesk, StudentLoan
This allows us to always recognise a class in code
This is a Java convention and must be followed; it creates self
documenting code – a requirement for OO
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
2
Basic Features
A Class has
Constructor(s) – a way to create objects
– Class ClassName() – default constructor
– Class ClassName(arg,arg) – alternate constructor
Attributes – properties which contain an object’s state
– DataType variableName
Method(s) – functions or procedures which determines an
objects’s behaviour using logic
– ReturnType methodName(arg,arg)
DataType and ReturnType can be int, float, double, boolean, String
etc
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Feature Visibility (Modifiers)
Attributes and methods can be:
Private features are only
visible to the class they
belong to and cannot be
inherited.
Public features are visible to
all classes and can be
inherited.
Protected features are
visible to the class they
belong to and all its
subclasses and can be
inherited – more later.
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Additional Concepts
• Instance variables defined by the class
• Hold property states ie data for an object
• The value of an attribute can change for each
Attributes object
• An attribute is created when the object is created.
• Its scope is the class: it is visible anywhere in the
class.
• Functions and procedures defined by the class
• Methods define behaviour of the objects of a class
• Functions return values, either by performing
Methods calculations or they can just return the value of an
attribute – they do not change anything
• Procedures are used to change the value of an
attribute
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
3
Naming attributes and methods
Always starts with lowercase, compound name is
Attributes and methods always start with a lowercase word
If the attribute or method has multiple words, each
subsequent word is letterCased – this is known as camelCase
For example, height, name, familyName, schoolStudent
Compound names should be used sparingly, most attributes
and methods should have a single word name
This is a Java convention and must be followed; it creates self
documenting code – a requirement for OO
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Designing Methods
What does • Method name
• Return type
it do? • Structure – sequence, selection, iteration
What do • Attributes
• Other methods
we have?
What do • Parameters
we need?
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Accessors and Mutators
Special types of methods which either:
• return the value of an attribute (unchanged) – Accessor
• or change the value of an attribute – Mutator
– Accessor Example
public String getName()
{ return name; }
where the name attribute is a String
– Mutator Example
public void setName(String newName)
{ name = newName; }
where name attribute is a String
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
4
Object Oriented Reuse
There are 3 types of reuse:
Method Reuse – a method can
be called with different variables
Object-based Reuse – an
instance of a class (an object) can
be created many times
Class-based Reuse – a parent
class can be inherited by many
child classes – more in week 3
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Method Reuse
A method can be called many times
Dog billy = new Dog ("M", "White", "Corgi", 11, true);
System.out.println(billy.sleep());
System.out.println(billy.bark());
System.out.println(billy.sleep());
System.out.println(billy.eat());
System.out.println(billy.sleep());
sleep() method reused
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Object Reuse
Many objects can be created from a class
Dog macca = new Dog ("M", "Tan", "Boxer", 4, false);
System.out.println(macca.bark());
System.out.println(macca.eat());
System.out.println(macca.sleep());
macca and billy objects both created from the Dog class
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
5
Data Types
• These data types are defined by the
Java language
• For example, int, double, boolean, char
Primitive • They are displayed in red
• They only hold data values, there is no
associated methods
• These are classes defined in the Java
Class Library or in your projects
• For example, String, Scanner, System
Reference • They always have a Capital Letter
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Classes and Data Types
The class name is the data type of the object
Dog is the data type of the Dog object macca
String is the data type of a String object
Scanner is the data type of a Scanner object
For example,
Scanner in = new Scanner(System.in);
String entry = in.nextLine();
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Constructors
A constructor (default and/or class) creates an object of a class
Here is an example of a constructor:
public Rental(int id, Customer customer)
{…}
It takes an int and a Customer object as parameters
We call it like this:
Rental newRental = new Rental(1, newCustomer);
Where newCustomer is a Customer object, created by a
Customer constructor
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
6
Assigning Attributes
Parameters to a constructor usually set the values of attributes
for an object
It is done inside the constructor
public Rental(int id, Customer customer)
{
this.id = id;
this.customer = customer;
}
this is a keyword in Java that refers to the current object ie the
object with datatype Rental that is being created by the constructor
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Assigning Attributes
We could have also done this
public Rental(int newId, Customer newCustomer)
{
id = newId;
customer = newCustomer;
}
Note: the parameters have different names to the attributes,
so we do not need to differentiate between the attribute (of
the object) and the constructor parameter.
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
A Format Function
A common way to format a double to 2 decimal places is to use
DecimalFormat
private String formatted(double amount)
{
DecimalFormat formatter =
new DecimalFormat("###,##0.00");
return formatter.format(amount);
}
DecimalFormat class belongs to java.text package, so you need
to use import java.text.*;
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
7
Key Concepts
Summary
A Class creates an Object
A Class has one or more
constructors, attributes and method(s)
A Constructor creates Objects
Attributes and methods can have
private, protected or public modifiers
Accessors and mutators provide
access to the attributes of a class
3 types of Reuse: method, object and
class
There are primitive data types and
class data types
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Review - Naming Conventions
A ClassName starts with a capital letter, for example, Dog
A constant is capitalised, for example, RATE
A variable starts with a lowercase, for example, name
A method name starts with a lowercase, for example, sleep()
Names should be simple
Use real words not abbreviations
Add useful inline comments to methods, if needed
Attributes, variables and methods are camelCased, for
example, firstName
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F