0% found this document useful (0 votes)
20 views

SE101 Lec2 ObjectsClasses (2)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

SE101 Lec2 ObjectsClasses (2)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Classes

OOP Concepts & Objects


implementation
Identity

States

Behaviors

Methods

Message
Passing

Constructors
 Some objects are different from each
other, some are similar.
 Each type of object has particular
attributes and behavior.
 The class is a specification of the
Classes attributes and behavior.
Properties? Actions?
 Example: caption set caption
position set position
size set size
font set font
... ...

Type?
Button OK
 Objects are instances of a class.
 So the class describes what an object
is like.
 An object is a concrete 'thing' that
has the properties and actions
Objects described by its class.
 An object is realised using the class to
btnExit
determine
 what it knows btnOK
 what it does.

Button
btnCancel
 Objects are not used in isolation.
Programm  Object-oriented programs are usually based
on a number of objects that work together
ing with to achieve the desired result.
objects  Some tasks would be difficult/impossible to
program without using an OO approach!
Collaborating objects
student[1]: Student this is a UML
object type
representation
name = Jones, Peter object id of an object
regNo = 10023456
dateOfBirth = 01/04/86
course[1]: Course
student[2]: Student
code = G567
name = Peters, Anne name = Software Engineering
regNo = 10023459 type = BSc Hons
dateOfBirth = 01/04/82 manager = Mary Squire

student[3]: Student current state


name = Anderson, of the object
Lynne
regNo = 10023478 This is a UML snapshot
dateOfBirth = 01/04/81 diagram: shows the current
link
between state of and links between
objects objects at a given moment in
time
 Object-oriented analysis & design:
 deciding what classes of objects are
needed
 writing class definitions
OO versus  writing applications that create and
conventio manipulate objects, passing
nal design messages between objects.
 Class:
 source – classname.java
Files,  compiled – classname.class

folders  Package:
 collection of classes, like a library
and  package name – all lower case

packages  source package – in project folder, in subfolder src\


packagename
 compiled – in project folder, in subfolder build\classes\
packagename
The project is
called
NetBeaCashTillExamp
le
ns
The classes folder
project contains a folder
files for each package,
which contain the
compiled classes The src folder
contains a folder
for each package,
these contain the
source code for the
classes.
 This function calculates and returns the square of a number
passed as a parameter:
function
type of value name
An to be returned parameter
type and
example name
Java int squareOf(int value) {
return value * value;
function }
(method) return the calculated
result
 Example of using the function:
area = squareOf(sideLength);
argument (value
passed to
 void – does something but no data value is returned
 Example:
void blankLines(int count) {
Function for (int line = 0; line < count; line ++)
System.out.println();
return }
types can  any Java type (including an object) – does something that
be ... needs to hand back data
 Example:
int squareOf(int num) {
return num * num;
}
 If no parameters are used, the brackets are required
but they are left empty; e.g.
double getPrice()

Function  When calling a function with no parameters, the


brackets must be used, but are left empty - ().
parameter
s  If there is more than one parameter, in the
definition use a comma as a separator, include the
type for each one, e.g.
int lower(int num1, int num2) { …
 Some objects are different from each
other, some are similar.
Classes -  Each type of object has particular
revisited attributes and behaviour.
 The class is a specification of the
attributes and behaviour.
 Class: create a source file and then type in the class
definition.

 Object: in an application that needs to use the


Creating object, use new to create the object.
Declare a variable of
classes type Rectangle, called
 For example, to create an object of a class called
shape.
and Rectangle:
objects Rectangle shape;
shape = new Rectangle();
Create a
Rectangle
object and place
it in the variable
called shape
 Three characteristics:
Three  identity
characteri  state
stics of  behaviour
objects
 Identity – not connected with the data values, but a
way of uniquely identifying an object.

 Usually use the address of the memory location that


Object references the object.

identity  For example:


Rectangle shape;
shape = new Rectangle(20, 40);

the variable shape


contains the identity
of the object
 An object will have a state, recorded by variables defined for
that class of object.
 An object has responsibilities - it needs to obey requests made
Object of it, sent as messages.
state and  An object's behaviour in response to a message is defined in a
method.
behaviour  So … an object has
 identity … a unique address,
 state … recorded in a set of variables,
 behaviour … defined as operations or messages.
 An object's class defines the attributes by
defining suitable variables.
 Attributes are also known as instance
Attributes variables – each instance (object) of the
class has its own copy of the variables.
in a class  For example, a class that records module
marks might have two attributes:
 courseworkMark
 examMark.
 The attributes for the module marks could be stored
in variables declared as:

Declaratio  private int coursework; These record


n of private int exam; an object's
state
variables
for  Each object (or instance) of the class would own two
variables as declared above.
attributes
 The variables are usually initialised by a special
method called a constructor.
 Example:
till.addItemPrice(item)
 The format of a message is:
receiver.selector(arguments)
Message  receiver: the name of the variable containing the object ( till)

format  selector: the name of the message being sent


(addItemPrice)
 arguments: if further data is required, the arguments pass this
on (item).
 Earlier, you saw two attributes declared for
module marks.
Methods
for the  Some possible methods:
 find out the coursework mark
marks  change the coursework mark
 find out the overall mark, given the weighting of
the two marks.
 Find out the coursework mark:
int getCoursework(){
return coursework; behaviour
}
 Change the coursework mark:
void setCoursework(int coursework){
The this.coursework = coursework;
}
function
 Find out the overall result:
definitions double getResult(int cWeight, int eWeight)
{
return (coursework * cWeight / 100.0
+ exam * eWeight / 100.0);
}
 Three main visibility modifiers:
 private – element only available within
its class (usually attributes)
public or  public – element freely available outside
private its class (usually methods)
 protected – used in sub-classes (later)
 Consider this:
Marks module1; identity

It declares a variable to hold a Marks object.

Initialising  To initialise the two instance variables it holds:


the module1 = new Marks(65, 60);
attributes
creates a new
Mark object, stores this tells it to use
its identity in the constructor to
variable module1 initialise the
instance variables
 The constructor needs two int parameters.
 It would be coded like this:
public class Marks {
private int coursework;
The private int exam;
public Marks(int coursework, int exam) {
constructo this.coursework = coursework;
r this.exam = exam ;
}
refers to the exam
attribute of this refers to the value
object passed by the parameter
called exam
Marks module1;

module1 = new Marks(65, 60);

The
constructo 0021
r - how it module
0021 is the 1 coursewor 65 0021
works identity of the exam
k 60 0025
object, i.e. the first
memory location
of its data storage.
This identity is Computer memory
stored in the
variable
module1.
 Must:
 have the same name as the class,
 not have a return type.

 It carries out any processing required to initialise


the attributes of the object.
Constructor
- the rules  If you wanted to create a Marks object but didn't
know the actual marks at the time, your constructor
would use default values:
public Marks() {
coursework = -1;
exam = -1;
}
 The system:
 checks the class of object and
determines its storage requirements,
 requests the necessary memory from
When an the OS
object is  allocates the memory for the attributes
 calls the constructor to initialise the
created
attributes as required
using new  returns the address of the memory,
… this is placed in the variable to which
the object is assigned (in case of
insufficient memory the value 'null' is
returned instead).
public class Marks
{
Class header
private int coursework;
private int exam; Attributes
public Marks() {
Putting it coursework = -1;
Constructor
together: exam = -1;
}
the class public int getCoursework()
: :
public void setCoursework(. . .)
Methods
: :
public double getResult(. . .)
}
NEXT CLASS:

More on OOP…..

Encapsulation
THANK YOU
Inheritance

Polymorphism

You might also like