Unit 9: Objects and Classes
Unit 9: Objects and Classes
1. Introduction
- Object-oriented programming is essentially a technology for developing reusable
software.
- To solve real-world problems using software, we’d like to define the real world’s
structure with attributes that exhibit the characteristics of the structure
- However, basic features like selections, loops, methods and arrays are not
sufficient for developing graphical user interfaces and large-scale software
systems.
=> The idea of Class and Objects comes into the picture in object-oriented
programming.
2. Object
- An object represents an entity in the real world that can be distinctly identified.
(ex: a student, a desk,…)
- An object consists of three unique things:
+ Identity: It gives a unique name to an object and enables one object to interact
with other objects.
+ State:
Known as its properties and attributes
Is represented by data fields with their current values
+ Behavior:
Known as its actions
Defined by methods (to invoke a method on an object is to ask the object to
perform an action)
- Example: “Dog” is a real-life object, which has some characteristics like color,
Breed, Bark, Sleep and Eat.
- Objects of the same type are defined using a common class.
3. Class
- A class is a template, blueprint, or contract that defines what an object’s data
fields and methods will be
- A class description consists of two things:
+ Attributes or member variables
+ Implementations of behavior or member functions.
// method 1
public String getName() { return name; }
// method 2
public String getBreed() { return breed; }
// method 3
public int getAge() { return age; }
// method 4
public String getColor() { return color; }
Output:
Hi my name is tuffy.
My breed,age and color are papillon,5,white
5. Constructing objects using constructors
- A constructor is invoked to create an object using the new operator
- Constructors are a special kind of method. They have three peculiarities (nét
riêng biệt):
+ A constructor must have the same name as the class itself.
+ Constructors do not have a return type—not even void
+ Constructors are invoked using the new operator when an object is created.
Constructors play the role of initializing objects
- Constructors can be overloaded (multiple constructors can have the same name
but different signatures)
- Example:
// Create a Main class
public Main() {
}
// Outputs 5
- A class is a reference type, which means that a variable of the class type can
reference an instance of the class
Circle myCircle;
The variable myCircle can reference a Circle object. The next statement creates an
object and assigns its reference to myCircle:
myCircle = new Circle();
6.2 Accessing and Object’s Data and Methods
- After an object is created, its data can be accessed and its methods can be invoked
using the dot operator (.), also known as the object member access operator:
+ objectRefVar.dataField references a data field in the object.
+ objectRefVar.method(arguments) invokes a method on the object
- Example: myCircle.radius references the radius in myCircle and myCircle.getArea() invokes the
getArea method on myCircle.
class Student {
class TestStudent {
- When creating Random object, we have to specify a seed or use the default seed.
A seed is a number used to initialize a random number generator.
- If two Random objects have the same seed, they will generate identical
sequences of numbers.
- Example:
Random generator1 = new Random(3);
System.out.print("From generator1: ");
for (int i = 0; i < 10; i++)
System.out.print(generator1.nextInt(1000) + " ");
Random generator2 = new Random(3);
System.out.print("\nFrom generator2: ");
for (int i = 0; i < 10; i++)
System.out.print(generator2.nextInt(1000) + " ");
Output:
From generator1: 734 660 210 581 128 202 549 564 459 961
From generator2: 734 660 210 581 128 202 549 564 459 961
7.3 The “Point2D” Class
- Java API has a convenient Point2D class in the javafx.geometry package for
representing a point in a two-dimensional plane.
- Example:
1 import java.util.Scanner;
2 import javafx.geometry.Point2D;
3
4 public class TestPoint2D {
5 public static void main(String[] args) {
6 Scanner input = new Scanner(System.in);
7
8 System.out.print("Enter point1's x-, y–coordinates: ");
9 double x1 = input.nextDouble();
10 double y1 = input.nextDouble();
11 System.out.print("Enter point2's x-, y–coordinates: ");
12 double x2 = input.nextDouble();
13 double y2 = input.nextDouble();
14
15 Point2D p1 = new Point2D(x1, y1);
16 Point2D p2 = new Point2D(x2, y2);
17 System.out.println("p1 is " + p1.toString());
18 System.out.println("p2 is " + p2.toString());
19 System.out.println("The distance between p1 and p2 is " +
p1.distance(p2));
20 System.out.println("The midpoint between p1 and p2 is " +
p1.midpoint(p2).toString());
21 }
22 }
Output:
9. Visibility Modifiers
- Visibility modifiers can be used to specify the visibility of a class and its
members
- The public visibility modifier can be used for classes, methods, and data fields to
denote they can be accessed from any other classes.
- A private method or data is accessible only inside the class
- If no visibility modifier is used, then by default the classes, methods, and data
fields are accessible by any class in the same package.
- This example illustrates how a public, default, and private data field or method in
class C1 can be accessed from a class C2 in the same package, and from a class C3
in a different package.
=> The private modifier restricts access to its defining class, the default modifier
restricts access to a package, and the public modifier enables unrestricted access.
- If a class is not defined as public, it can be accessed only within the same
package.
C1 can be accessed from C2, but not from C3
- An object can access its private members if it is defined in its own class.
+ To make a private data field accessible, provide a getter method to return its
value.
+ To enable a private data field to be updated, provide a setter method to set a new
value
- A getter method has the following signature:
public returnType getPropertyName()
+ If the returnType is boolean, the getter method should be defined as follows by
convention:
public boolean isPropertyName()
- A setter method has the following signature:
public void setPropertyName(dataType propertyValue)
The line this(1.0) in the second constructor invokes the first constructor with a
double value argument