Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects Real and Java COMP T1 3

Similar presentations


Presentation on theme: "Objects Real and Java COMP T1 3"— Presentation transcript:

1 Objects Real and Java COMP 112 2017T1 3

2 Real Objects The world contains many real objects for example:
Student objects Course objects The world has different types or classes of things. Different classes of object can do different things Different objects can be in different states. Many objects can be of the same class. A student is not the same as a course Students go to labs courses do not. Each student has a unique ID. There is more that one student.

3 Java Objects A Class defines what an object can do
A Class is a collection of: private data (fields) can be integers…. or an Object public methods Each object has its own fields that define its state The methods defined for a class can be applied to an object of that class and can change the state of the object. Both fields and methods live on an object But static fields are are shared by all objects and static methods can be executed from the class ! Each student object must have a unique ID! Code a Student Class that enforces this.

4 Objects in Java Foo.java defines the class Foo
Define Foo related things in Foo.java Fields (hold data) Methods (can be executed) Main method executed when Foo executed Constructor methods are used to build objects Have same name as Class Have no return type Used to construct Object of the Class Best understood by examples.

5 Example Program Ex1.java
----- Meeting Notes (23/02/15 16:07) ----- 1. Each object has differnt fields but same type of fields 2. Class name Ex1 3. Constructor definition 4. Constructor execution 5. Method definition 6. Method calling - on the object

6 AddCounter.java ----- Meeting Notes (23/02/15 16:11) -----
Note: Two objects each with different name and value.

7 BlueJ - Classes and Objects
Right click (on class) New AddCounter (to make an object) Classes

8 BlueJ Creating Objects

9 BlueJ In BlueJ Right click on object to call method Object
BlueJ allows any method to be executed. Helpful for testing ----- Meeting Notes (9/03/15 08:23) ----- Two kinds of things 1. Classes on which you may executs static methods 2. Objects on which you may execute (non-static) methods Object

10 Primitives and Objects
int x = 1; x names a piece of memory in which the value 1 is stored X Car z = new Car(2); z Car21 Car21 x 2 The value in z is the reference to the Object.

11 Method calling Primitive parameters are values.
The value in an int is passed But the reference to Object Param is passed Pass a value Pass a reference ----- Meeting Notes (8/03/14 18:40) ----- Information flows into a method with value passing Information flows both ways with referance passing setx on p Pass value getx on p

12 Overview of what is to come
Object are: Of Reference type Compared by myOb.equal(anotherOb) but you write the “.equal” methods! myOb = anotherOb assignment (makes references the same) myOb == anotherOb test (is same reference) Parameter passing Automatic Boxing and type conversion BlueJ debugging Use .equal not == Until you understand

13 Passing Objects A variable of Object type is a pointer.
The value passed when an Object is a parameter is the pointer. state change ----- Meeting Notes (8/03/14 18:49) ----- inx by value p by reference Although method is run on Object mp the Object mq is changed ----- Meeting Notes (24/02/15 08:00) ----- Recall information flows BOTH ways with referance parameters ----- Meeting Notes (8/03/15 16:50) ----- *** is one OR two changed by one.oMethod(two,30) two is changed

14 Reference type Car z = new Car(2); Car y = new Car(4); z Car21 Lv 2
y Car23

15 Reference type Oby21 lv 2 Obj z = new Oby(2); Obj y = new Oby(1); Obj x = new Oby(1); z Oby21 Oby25 lv 1 y Oby25 x==y false x.equals(y) true z==y false ----- Meeting Notes (8/03/15 16:50) ----- What is == and what is .equals ?? ----- Meeting Notes (9/03/15 08:23) ----- Remember what x,y,z point to NOW execute x=y. What happens? Oby29 lv 1 x Oby29 You have to write the .equals()method What does y = z; do to the data above?

16 Reference type After y=z; Oby21 z Oby21 Oby25 y Oby21 Oby29 x Oby29
x==y false x.equals(y) false z==y true

17 First attempt at the equals method
.equal() method in the ObjMeth class ----- Meeting Notes (8/03/14 18:06) ----- Compiler Objects Later you find out that Run time errors can occur! this.x is x in this object What happens when a different kind of Object is passed as a parameter?

18 The equals Method Accepts any Object
----- Meeting Notes (8/03/14 18:08) ----- This equals method is USED by System when Hashing ----- Meeting Notes (9/03/14 11:41) ----- Explain instanceof Accepts any Object Checks the object is of the correct type

19 Method calling Methods can be distinguished by:
return type, a name and a list of parameter types More than one method with same name can be defined. Two Methods can be defined with same name but different return types (or) parameter types Hence both equals(Object x) and equals(ObjMeth x) can be defined

20 Parameter passing with Objects
Oby z = new Oby(2); o.meth(z); … } void meth(Oby p){ p.setx(4) …. z Oby21 Oby21 x 2 p Oby21 ----- Meeting Notes (24/02/15 08:09) ----- 1. Build new Object z 2. p is set to point to the same object as z 3. seting the x value in p CHANGES z

21 Parameter passing Obj z = new Oby(2); o.meth(z); … void meth(Obj p){ p.setx(4) z Oby21 Oby21 x 4 p Oby21 both p and z have been changed

22 Boxing in java Integer to int int to Integer Integer to int to String
Converting a primitive type into an Object is Boxing Can be performed manually or by the compiler Automatic type conversion helps you code Integer to int int to Integer Integer to int to String

23 Debugging write display statements in the code run the program
One of the simplest (oldest) ways to test/debug software is: write display statements in the code run the program Inspect the output IDEs come with some built in features that can help

24 BlueJ Debug Window Method call Sequence Variables Step Into Call
Step Over Call

25 Inspecting Variables Double click on a variable.


Download ppt "Objects Real and Java COMP T1 3"

Similar presentations


Ads by Google