Week 2 - Lectures 1 and 2 - Chapter 8
Week 2 - Lectures 1 and 2 - Chapter 8
1
8.2 Defining Classes for Objects
What is an object?
Object-oriented programming (OOP) involves
programming using objects. An object represents
an entity in the real world. For example, a student,
a desk, a circle, and even a loan can all be viewed
as objects.
An object has a unique state, and behaviors. The
state of an object consists of a set of data fields
(also known as attributes) with their current values.
The behavior of an object is defined by a set of
2
methods.
8.2 Defining Classes for Objects
What is a class?
Classes are constructs that define objects of the
same type. A Java class uses variables to define
data fields and methods to define behaviors.
Additionally, a class provides a special type of
methods, known as constructors, which are
invoked to construct objects from the class.
3
8.2 Defining Classes for Objects
7
8.3 Example: Defining Classes
and Creating Objects
10
•
Creating an object
We create a circle object using one the following two
statements:
Circle circle1 = new Circle();
Circle circle2 = new Circle(25);
• Where Circle which is the class name is also known as the
type (reference type), circle1 is the variable (a.k.a. object
reference variable), new is a keyword, and Circle () /
Circle(25) is a call to the constructor.
11
Creating an object (continued)
Circle circle1 = new Circle(25);
13
Example Application
public class Application {
public static void main(String[] args) {
Circle circle1 = new Circle(); // Create a circle with radius 1
// print the area of circle 1
System.out.println("The area is " + circle1. getArea() );
Circle circle2 = new Circle(25); // Create a circle with radius 25
// print the area of circle 2
System.out.println("The area is " + circle2. getArea() );
// print the perimeter of circle 2
System.out.println("The perimeter is " + circle2. getPerimeter() );
}
14
}
8.4 Constructing Objects Using
Constructors
Constructors are a special kind of methods that are
invoked to construct objects.
public Circle() { // No-arg constructor
radius =1;
}
17
8.5 Accessing Objects via Reference Variables
Types
yourCircle.radius = 100;
21
animation
Trace Code, cont.
radius: 5.0
Create a circle
22
animation
Trace Code, cont.
23
animation
Trace Code, cont.
Circle myCircle = new Circle(5.0); reference value
myCircle
Circle yourCircle = new Circle();
radius: 5.0
yourCircle no value
Declare yourCircle
24
animation
Trace Code, cont.
Circle myCircle = new Circle(5.0); reference value
myCircle
Circle yourCircle = new Circle();
radius: 5.0
yourCircle no value
: Circle
Create a new
Circle object radius: 1.0
25
animation
Trace Code, cont.
Circle myCircle = new Circle(5.0); reference value
myCircle
Circle yourCircle = new Circle();
radius: 5.0
radius: 1.0
26
animation
Trace Code, cont.
Circle myCircle = new Circle(5.0); reference value
myCircle
Circle yourCircle = new Circle();
radius: 5.0
: Circle
Change radius in
radius: 100.0
yourCircle
27
Caution
Can you invoke getArea() using Circle.getArea()? The answer is no. All
the methods used before this chapter are static methods, which are
defined using the static keyword. However, getArea() is non-static. It
must be invoked from an object using
29
The null Value
Try running the code below after writing the class Student.
public class Test {
public static void main(String[] args) {
Student student = new Student();
System.out.println("name? " + student.name);
System.out.println("age? " + student.age);
System.out.println("isScienceMajor? " + student.isScienceMajor);
System.out.println("gender? " + student.gender);
}
}
31
Caution
Java assigns no default value to a local variable
inside a method. As discussed earlier, Java only
assigns default values to data fields in a class.
public class Test {
public static void main(String[] args) {
int x; // x has no default value
String y; // y has no default value
System.out.println("x is " + x);
System.out.println("y is " + y);
}
}
radius = 1
33
Copying Variables of Primitive Data
Types and Reference Types
Primitive type assignment i = j
Before: After:
i 1 i 2
j 2 j 2
Before: After:
c1 c1
c2 c2
35