Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Object-Oriented Software Engineering CS288. 2 Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating.

Similar presentations


Presentation on theme: "1 Object-Oriented Software Engineering CS288. 2 Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating."— Presentation transcript:

1 1 Object-Oriented Software Engineering CS288

2 2 Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating fields Manipulating object field values Static Declarations

3 3 Classes and Objects Class: Template that defines how to make an object. Object: Is an instance of a class. Car int numDoors Double engineSize String make void start ( ) { code } void stop ( ) { code } Car bigFlashJag numDoors = 5 engineSize = 3.5l make = Jaguar start ( ) stop ( ) Car ecoCar numDoors = 2 engineSize = 0.5l make = Smart start ( ) stop ( ) ClassObject

4 4 Building Objects Computer Memory Class Definition (acts like template for new objects) Object1Object2Object3Object4 Each object has copies of fields and methods defined by class template, but initialised differently each time

5 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 set up. Constructor, defines how new objects are initialised.

6 6 Syntax for defining new object SimpleClass ob1 = new SimpleClass("Fluffy"); object type object name new keyword class constructor to set up object semicolon terminates ALL statements (just like C++)

7 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 8 Executing the Code Five new objects are created in memory, each with their own copy of ‘uselessField’, and each copy has a different value. Slide shows view of data from NetBeans debugging console.

9 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 10 Getter method for field public String getUselessField () { return uselessField; } public keyword allows 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 11 Setter method for field public void setUselessField (String newUselessField) { uselessField = newUselessField; } has String object parameter does not return value changes value of field

12 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 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 here objects have these values

14 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 here objects have these values

15 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 here objects have these values

16 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 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 here objects have these values

18 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 here objects have these values

19 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 here objects have these values

20 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 here objects have these values

21 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 here objects have these values

22 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 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 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: SimpleClass field: uselessField Object: SimpleClass("Fluffy") field:

25 25 Static Declarations Changing the value of the field within one object now affects all the other objects 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 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 error BUILD FAILED (total time: 0 seconds)

27 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 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 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 from the class. (Notice that the main method is always declared static. WHY?)

30 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 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 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 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 objects name.

34 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 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 other methods that call setUselessField:

36 36 `this’ keyword `this’ can also be used to make methods more readable. When a method argument is to be used to update an existing field, then it is often the case that the argument is given the same name as the field name. This leads to a conflict that can 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 37 Summing Up Every object has copies of the class fields and 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.


Download ppt "1 Object-Oriented Software Engineering CS288. 2 Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating."

Similar presentations


Ads by Google