Download presentation
Presentation is loading. Please wait.
Published byHope Pearson Modified over 9 years ago
1
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP 112 2015T1 3
2
COMP 112 3: 2 © D.Streader Real Objects The world contains many real objects for example: Student objects Course objects 1.The students are of a different type or class to the courses. 2.Different classes of object can do different things 3.Different objects can be in different states. 4.Many objects can be of the same class.
3
COMP 112 3: 3 © D.Streader 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 an change the state of the object. A student record system might have: 1.A Student class and a Student object for each student. 2.A Course class and a Course object for each course 3.A course method could be Enroll that takes a student and enrolls them on the course.
4
COMP 112 3: 4 © D.Streader 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 an example.
5
COMP 112 3: 5 © D.Streader Example Program Ex1.java
6
COMP 112 3: 6 © D.Streader AddCounter.java
7
COMP 112 3: 7 © D.Streader BlueJ - Classes and Objects Classes Right click (on class) New AddCounter (to make an object)
8
COMP 112 3: 8 © D.Streader Creating Objects BlueJ
9
COMP 112 3: 9 © D.Streader In BlueJ Right click on object to call method BlueJ allows any method to be executed. Helpful for testing BlueJ Object
10
COMP 112 3: 10 © D.Streader Primitives and Objects int x = 1; x 1 x names a piec e of memory in which the value 1 is stored Car z = new Car(2); z Car21 Car21 Fx 2 The value in z is the reference to the Object.
11
COMP 112 3: 11 © D.Streader Method calling Primitive parameters are values. The value in an int is passed But the reference to Object Param is passed Pass value Pass a reference Pass a value setx on p getx on p
12
COMP 112 3: 12 © D.Streader Overview of what is to come Object are : 1.Of Reference type 2.Compared by myOb.equal(anotherOb) but you write the “.equal” methods! 3. myOb = anotherOb assignment (makes references the same) 4. myOb == anotherOb test (is same reference) Parameter passing Automatic Boxing and type conversion BlueJ debugging Use.equal not == Until you understand
13
COMP 112 3: 13 © D.Streader Passing Objects A variable of Object type is a pointer. The value passed when an Object is a parameter is the pointer. two is changed
14
COMP 112 3: 14 © D.Streader Reference type Car21 Car z = new Car(2); Lv 2 z Car21 Car23 Car y = new Car(4); Lv 4 y Car23
15
COMP 112 3: 15 © D.Streader Reference type Obj z = new Oby(2); Obj y = new Oby(1); Obj x = new Oby(1); x==y false x.equals(y) true z==y false What does x = y; do to the data above? z Oby21 y Oby25 x Oby29 You have to write the.equals() methods Oby21 lv 2 Oby25 lv 1 Oby29 lv 1
16
COMP 112 3: 16 © D.Streader Reference type x==y false x.equals(y) true z==y true After z=y; Oby21 2 z Oby25 Oby25 1 y Oby25 Oby29 1 x Oby29
17
COMP 112 3: 17 © D.Streader First attempt at the equals method What happens when a different kind of Object is passed as a parameter?.equal() method in the ObjMeth class this.x is x in this object
18
COMP 112 3: 18 © D.Streader The equals Method Accepts any Object Checks the object is of the correct type
19
COMP 112 3: 19 © D.Streader 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 1.return types (or) 2.parameter types Hence both equals(Object x) and equals(ObjMeth x) can be defined
20
COMP 112 3: 20 © D.Streader Parameter passing with Objects Obj z = new Oby(2); o.meth(z); ….. void meth(Obj p){ p.setx(4) …. p Oby21 z Oby21 Oby21 x 2
21
COMP 112 3: 21 © D.Streader Parameter passing Obj z = new Oby(2); o.meth(z); ….. void meth(Obj p){ p.setx(4) …. z Oby21 Oby21 x 4 p Oby21 z has been changed by calling o.meth(z)
22
COMP 112 3: 22 © D.Streader Boxing in java 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
COMP 112 3: 23 © D.Streader Debugging One of the simplest (oldest) ways to test/debug software is: 1. write display statements in the code 2.run the program 3.Inspect the output IDEs come with some built in features that can help
24
COMP 112 3: 24 © D.Streader BlueJ Debug Window Method call Sequence Variables Step Over Call Step Into Call
25
COMP 112 3: 25 © D.Streader Inspecting Variables Double click on a variable.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.