Presentation is loading. Please wait.

Presentation is loading. Please wait.

UniS 1 Object-Oriented Software Engineering CS288.

Similar presentations


Presentation on theme: "UniS 1 Object-Oriented Software Engineering CS288."— Presentation transcript:

1 UniS 1 Object-Oriented Software Engineering CS288

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

3 UniS 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 UniS 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 UniS 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 UniS 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 UniS 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 UniS 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.

9 UniS 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 UniS 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 UniS 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 UniS 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); }

13 UniS 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 UniS 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 UniS 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 UniS 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 UniS 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 UniS 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 UniS 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 UniS 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 UniS 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 UniS 22 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.


Download ppt "UniS 1 Object-Oriented Software Engineering CS288."

Similar presentations


Ads by Google