1 Object-Oriented Software Engineering CS288. 2 Java OO Fundamentals Contents Classes and Objects...
-
Upload
wesley-allen -
Category
Documents
-
view
228 -
download
2
description
Transcript of 1 Object-Oriented Software Engineering CS288. 2 Java OO Fundamentals Contents Classes and Objects...
1
Object-Oriented Software Engineering
CS288
2
Java OO Fundamentals
Contents• Classes and Objects• Making new objects• Method declarations• Encapsulating fields• Manipulating object field values• Static Declarations
3
Classes and Objects
Class: Template that defines how to make an object.Object: Is an instance of a class.
Car
int numDoorsDouble engineSizeString makevoid start ( ) { code }void stop ( ) { code }
Car bigFlashJag
numDoors = 5engineSize = 3.5lmake = Jaguarstart ( )stop ( )
Car ecoCar
numDoors = 2engineSize = 0.5lmake = Smartstart ( )stop ( )
Class
Object
Object
4
Building Objects
Computer MemoryClass Definition(acts like template for
new objects)
Object1 Object2 Object3 Object4
Each object has copies of fields and methods defined by class template, but initialised differently each time
5
Very Simple Class Example
public class SimpleClass {
public SimpleClass(String newFieldVal) { uselessField = newFieldVal; }
private String uselessField;
public static void main(String[ ] args) { /* Code Goes Here */ }
}Main method executed first.
This is where objects can first be setup.
Constructor, defines how new objectsare initialised.
6
Syntax for defining new object
SimpleClass ob1 = new SimpleClass("Fluffy");
object type object name
new keyword
class constructor to set up object semicolon terminatesALL statements(just like C++)
7
Very Simple Class Example
public class SimpleClass {
public SimpleClass(String newFieldVal) { uselessField = newFieldVal; }
private String uselessField;
public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); }
}
8
Executing the Code
Five new objects are created in memory, each with their owncopy of ‘uselessField’, and each copy has a different value.Slide shows view of data from NetBeans debugging console.
9
Setter and Getter Methods
public class SimpleClass { /* declare class field */ /* define class constructor */ public String getUselessField () { return uselessField; }
public void setUselessField (String newUselessField) { uselessField = newUselessField; } /* main method */}
10
Getter method for field
public String getUselessField () { return uselessField; }
public keywordallows this method to be called from other classes
type of object returned by method
name and parameters of method
body of method,what it does
11
Setter method for field
public void setUselessField (String newUselessField) { uselessField = newUselessField; }
has String object parameterdoes not return value
changes value of field
12
Extending main method
public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); }
Example shows use of setting and getting methods for uselessField
13
Execution of main method public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); }
When execution gets to hereobjects have these values
14
Execution of main method public static void main(String[] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); }
When execution gets to hereobjects have these values
15
Execution of main method public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); }
When execution finishes hereobjects have these values
16
Backup Method for Field Value
Modify SimpleClass to include new field and methods:
private String previousVal;
public void setUselessField (String newUselessField) { previousVal = uselessField; uselessField = newUselessField; }
public void restoreField () { uselessField = previousVal; }
17
Experiment with main method
public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); } When execution gets to
hereobjects have these values
18
Experiment with main method
public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); } When execution gets to
hereobjects have these values
19
Experiment with main method
public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); }
When execution gets to hereobjects have these values
20
Experiment with main method
public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); }
When execution gets to hereobjects have these values
21
Is there a problem with the restore method?
public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); ob1.setUselessField ("I wish I'd done C instead"); ob2.restoreField (); }
When execution gets to hereobjects have these values
22
Static Declarations
• Not all methods and fields belong to Objects• Static methods and fields stay inside the class• There is only ever one copy of a static method or field• Static methods and fields are `shared’ between all
the objects of that class
23
Static Declarations
• When we declare a new variable with a statement we create a reference to the thing we want the variable to name
• E.g SimpleClass ob1 = new SimpleClass("Fluffy");SimpleClass ob1 = ob2;
Object:SimpleClass("Fluffy")
ob1 ob2
24
Static Declarations
• Static fields stay with the class not the object• E.g change declaration in SimpleClass
private static String uselessField = "oooo";• Then in main method create two objects:
SimpleClass ob1 = new SimpleClass("Fluffy");SimpleClass ob1 = new SimpleClass("Fluffy");
Object:SimpleClass("Fluffy")field:
ob1 ob2
Class: SimpleClassfield: uselessField
Object:SimpleClass("Fluffy")field:
25
Static Declarations
Changing the value of the field within one object now affects all the otherobjects of that class:
SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField ();
NetBeans Debugger Output
26
Static Methods
• Static methods are more complex than static fields• This is NOT allowed:
public class SimpleClass { private String uselessField = "oooo"; private String initialVal = ""; public static void setUselessField(String newUselessField) { uselessField = newUselessField; } /* and so on */
If we attempt to compile this we get error:SimpleClass.java: non-static variable uselessField cannot be referenced from a static context uselessField = newUselessField;1 errorBUILD FAILED (total time: 0 seconds)
27
Static Methods
• Static methods are more complex than static fields• E.G. this is NOT allowed:
public class SimpleClass { private String uselessField = "oooo"; private String initialVal = ""; public static void setUselessField(String newUselessField) { uselessField = newUselessField; } /* and so on */
Static methods may only refer to static fields.Whereas here uselessField is NOT declared static.
WHY NOT?
28
Static Methods
• Static methods are more complex than static fields• E.G. this is NOT allowed:
public class SimpleClass { private static String uselessField = "oooo"; private String initialVal = ""; public static void setUselessField(String newUselessField) { uselessField = newUselessField; } /* and so on */
Now uselessField is declared static. Code compiles without error.
29
Static Methods
• Can reference static methods and fields directly from the class,do not need to create an object first.
• E.G. public static void main(String[ ] args) {
SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("Fluffy"); SimpleClass.setUselessField ("I wish I'd done C instead"); }
To use the static method here we can access it directly fromthe class.(Notice that the main method is always declared static. WHY?)
30
Static Methods
Consider adding a new method to store previous value used for uselessField:
Consider adding a new method to store previous value used for uselessField, which needs a new field to hold that value in:
private String lastValueField;
public void storeLastValue () {
lastValueField = uselessField;
}
Then could use this whenever set value of uselessField so that we can track the last value
31
Static Methods
Then could use this whenever set value of uselessField so that we can track the last value.
public static void setUselessField(String newUselessField) { uselessField = newUselessField; /* this keeps track of the last value used for uselessField */ storeLastValue(); }
Problem: Wont Compile, get error:
non-static method storeLastValue() cannot be referenced from a static context: storeLastValue();
32
Static Methods
Problem:
• We must have setUselessField declared static for some reason.
• For some other reason we must have that storeLastValue is not declared static.
• But we cant access storeLastValue in method body setUselessField of as it is not static.
33
Static Methods
Solution:
public static void setUselessField(String newUselessField, SimpleClass obj) { uselessField = newUselessField; obj.storeLastValue(); } public void storeLastValue ( ) { lastValueField = uselessField; }
Adds new argument
Now refer to method via objectsname.
34
`this’ keyword
New Problem, the constructor method.
public SimpleClass( ) { setUselessField("Some String", ????????); initialVal = uselessField; }
What value goes here?
Solution: keyword `this’`this’ is a reference to the current object.The object whose method or constructor is being called.
35
`this’ keyword
public SimpleClass( ) { setUselessField("Some String", this); initialVal = uselessField; } public void resetField() { setUselessField(initialVal, this); } public SimpleClass(String newUslessField) { setUselessField(newUslessField, this); }
Reference with `this’ must then be introduced into othermethods that call setUselessField:
36
`this’ keyword`this’ can also be used to make methods more readable.When a method argument is to be used to update an existingfield, then it is often the case that the argument is given the same name as the field name. This leads to a conflict thatcan be resolved by using `this’.
For example instead of: public static void setUselessField(String newUselessField, SimpleClass obj) { uselessField = newUselessField; obj.storeLastValue(); }
we can have: public static void setUselessField(String uselessField, SimpleClass obj) { this.uselessField = uselessField; obj.storeLastValue(); }
37
Summing Up
• Every object has copies of the class fieldsand methods.
• We have seen example field values for multiple objects.• Seen examples of methods for accessing and changing
field values.• We have seen how objects are created and manipulated
during execution of the main method.• Seen how static methods and fields are only created once
within the class and are not copied to new objects.