[Java] Java371 Lecture 6
Object−Oriented Programming (OOP)
info
This document is a class note about Java Programming taughted by Mr.Lu.
Object & Class
- An Object is an entity to maintain its own states in fields(attributes, properties...) and provide accesory methods on fields.
- To create objects of this type, we define a new class as follows:
- designate a name with the first letter capitalized.
- declare data and function members in the class body.
- Note that a class is one way to create reference types.(ex. interface and enum).
- In this sense, defining a new class is to define a new type!
Example: Points
- For any 2D point, the class could look like the code snippet below
public class Point {
// Data members.
double x, y;
}
Then we manipulate two points in another class, shown in the next page.
public class PointDemo {
public static void main(String[] args) {
Point p1 = new Point();
p1.x = 1;
p1.y = 2;
Point p2 = new Point();
p2.x = 3;
p2.y = 4;
System.out.printf("(%.2f, %.2f)\n", p1.x, p1.y);
System.out.printf("(%.2f, %.2f)\n", p2.x, p2.y);
}
}
Encapsulation
- Each member may have an access modifier, say public and private.
- public: accessible by all classes.
- private: accessible only within its own class.
- In OOP practice, the internals like data members should be isolated from the outside world.
- So all fields should be declared private.
- Note that the private modifier does not guarantee any information security.
- What private is good for maintainabiliy and modularity.
- We then expose the public methods which perform actions on these fields, if necessary.
- For example,
- getter: return one specific field.
- setters: assign new value to the field.
- For example, getX() and getY() are the getters; setX() and setY() are the setters of the Point class.
Example: Point (Encapsulated)
public class Point {
// Data members: fields or attributes
private double x, y;
// Function memebers: methods
public double getX() { return x; }
public double getY() { return y; }
public void setX(double a) { x = a; }
public void setY(double b) { y = b; }
}
Constructors
- To create an object of the type, its constructor is invoked by the new operator.
- You can define constructors with parameters if necesary.
- For example, one can initialize the object during the creation.
- Note that a constructor has its name identical to the class name and has no return type.
- If you don't define any explicit constructor, Java assumes a default constructor for you.
- Adding any explicit constructor disables it but you can recover it by yourself.
Parameterized Constructors: Example
public class Point {
...
// Default constructor
public Point() {
// Do something in common
}
// Parameterized constructor
public Point(double a, double b) {
x = a;
y = b;
}
}
- You can initialize an object when the object is allocated.
Self Reference: this
- You can refer to any (instance) member of the current object by using this, within its (instance) methods and constructors.
- The most common situation to use this is that a field is shadowed by method parameters.
- It is a direct result of the shadow effect.
- You can also use this to call another constructor of the class, say this() calling the default constructor, if existing.
Example: Point (Revisited)
public class Point {
...
public Point (double x, double y) {
this.x = x;
this.y = y;
}
}
- However, the this operator cannot be used in static methods.
Instance Members
- Notice that all members of the class are declared w/o static since we start this leture.
- These members are called instance members, available only after one object is created.
- Semantically, each object has its own states, associated with the accessory methods applying on.
- For example, getX() could be invoked and return the x value for some specific Point object.
- In other words, you cannot invoke getX() without an existing Point object.
Static Members
- A static variable occupies only one space, shared among the class and its objects.
- You can refer to these static members by calling the class name in absence of any instance.
- For example, Math.PI.
- In particular, static methods perform algorithms.
- For example, Math.random() and Arrays.sort().
Example: Distance Between Points
public class Point {
public double getDistanceFrom(Point that) {
return Math.sqrt(Math.pow(this.x - that.x, 2))
+ Math.pow(this.y - that.y, 2));
}
public static double measure(Point first, Point second) {
return Math.sqrt(Math.pow(first.x − second.x, 2)
+ Math.pow(first.y − second.y, 2));
}
}
public class PointDemo {
public static void main(String[] args) {
System.out.println(p1.getDistanceFrom(p2));
System.out.println(Point.measure(p1, p2));
}
}
- Both methods produce the same result.
- It concludes that
- if the object keeps its own states, then declare non-static variables for those;
- one can deal with data with both static or non-static methods.