02 OOP ClassesAndObjects
02 OOP ClassesAndObjects
July 2012
1
Class
Object
More on class
Enum types
Declarations for the fields that provide the state of the class
You can add access modifiers like public, protected, private at the
beginning, which determine what other classes can access MyClass
(discuss later).
The Bicycle class uses the following lines of code to define its fields:
int cadence;
int speed;
int gear;
All variables follow the same naming rules and conventions defined
by the Java language.
The same naming rules and conventions are used for methods and
class names, except that
2. The return type or void if the method does not return a value;
10
11
13
14
15
A parameter can have the same name as one of the classs fields, which is
conventionally used only within constructors.
16
17
This means that when the method returns, the passed-in reference still
references the same object as before.
18
When the method returns, myCircle still references the same Circle
object as before the method was called.
20
Class
Object
More on class
Enum types
22
You must create and assign an object to originOne before you use it in
your code; otherwise you will get a compilation error.
Heres the code for the Point class which has a single constructor:
public class Point {
public int x = 0;
public int y = 0;
public Rectangle() {
origin = new Point(0, 0);
The class Rectangle has 4 constructors. }
public Rectangle(Point p) {
origin = p;
Each constructor lets you provide }
public Rectangle(int w, int h) {
initial values for the rectangles size origin = new Point(0, 0);
width = w;
and origin, using both primitive and height = h;
reference types. }
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
If a class has multiple constructors, height = h;
}
they must have different signatures. public void move(int x, int y) {
origin.x = x;
origin.y = y;
The Java compiler differentiates the }
public int getArea() {
constructors based on the number and return width * height;
}
the type of the arguments. }
25
//...
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
}
26
Use a simple name for a field within its class, like this:
System.out.println("Width and height are: " + width + ", " + height);
27
In the code outside Rectangle class and if we have an Rectangle object named
rectOne:
System.out.println("Width of rectOne: " + rectOne.width);
System.out.println("Height of rectOne: " + rectOne.height);
Recall that the new operator returns a reference to an object. So we could use the
value returned from new to access a new objects field:
After executing this statement, the program has no longer reference to this created
Rectangle, because the program never stored the reference anywhere (GC).
28
The Rectangle class has two methods: move(int, int) to change the
rectangles origin, and getArea() to compute the rectangles area.
System.out.println("Area of rectOne: " + rectOne.getArea());
// ...
rectTwo.move(40, 72);
29
The Java platform allows you to create as many objects as you want
and you dont have to worry about destroying them.
31