Professional Documents
Culture Documents
Topic 1 Introduction To Object Orientation
Topic 1 Introduction To Object Orientation
Circle Object 3
Data fields:
radius is 125
Methods:
getArea()
getPerimeter()
Objects – Example 2
• A remote control unit is an object.
• A remote control object has three attributes:
– The current channel, an integer,
– The volume level, an integer, and
– The current state of the TV, on or off, true or false
Notation:
In data field dataFieldName: dataFieldType
Circle(double newRadius) {
radius = newRadius;
}
Constructors (Cont.)
• Three peculiarities
– Constructors 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.
• A constructor with no parameters is referred to as a
no-arg constructor.
• Constructors can be overloaded(multiple constructors
can have the same name but different signature)
Constructors (Cont.)
• A class may be declared without constructors.
• In this case, a no-arg constructor with an
empty body is implicitly declared in the class.
• This constructor, called a default constructor,
is provided automatically only if no
constructors are explicitly declared in the
class.
Classes – Exercise
Rectangle Class
• A Rectangle class might specify that every Rectangle object consists of two variables
of type double:
– double length, and
– double width,
• Individual Rectangle objects may differ in dimension but all Rectangle objects share
the same methods
• Question:
– Draw a UML class diagram of rectangle class
– Create a rectangle class
1.3 Constructing Objects Using
Constructors
• Creating Objects Using Constructors
Syntax:
new ClassName(); // default constructor
new ClassName(parameter);
Example:
new Circle();
new Circle(25);
1.4 Accessing Objects via Reference Variables
Example:
Circle myCircle;
The variable myCircle can reference a Circle object
Declaring/Creating Objects in a Single Step
Syntax:
ClassName objectRefVar = new ClassName();
Example:
Assign object reference
Create an object
Accessing Objects data and Methods
• Data field can be accessed and its methods invoked using the
dot operator (.)
– known as object member access operator.
yourCircle.radius = 100;
Trace Code, cont.
Create a Circle
Trace Code, cont.
yourCircle.radius = 100;
: Circle
Assign object radius: 5.0
reference to myCircle
Trace Code, cont.
myCircle reference value
yourCircle no value
Declare yourCircle
Trace Code, cont.
myCircle reference value
yourCircle no value
: Circle
radius: 1.0
Create a new
Circle object
Trace Code, cont.
myCircle reference value
Assign object
: Circle
reference to
yourCircle radius: 1.0
Trace Code, cont.
Circle myCircle = new Circle(5.0); reference value
myCircle
Circle yourCircle = new Circle();
: Circle
Change radius in radius: 100
yourCircle
Reference Data Fields
The data fields can be of reference types. For
example, the following Student class contains a
data field name of the String type
public class Student {
String name; // name has default value null
int age; // age has default value 0
boolean isScienceMajor; // isScienceMajor has default value
false
char gender; // c has default value '\u0000'
}
The null Value
• If a data field of a reference type does not
reference any object, the data field holds a
special literal value, null.
Default Value for a Data Field
The default value of a data field is null for a reference
type, 0 for a numeric type, false for a boolean type, and
'\u0000' for a char type. However, Java assigns no
default value to a local variable inside a method.
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);
}
}
Differences between Variables of
Primitive Data Types and Object Types
Copying Variables of Primitive Data Types
and Object Types
Garbage Collection
• As shown in the previous figure, after the assignment
statement c1 = c2,
– c1 points to the same object referenced by c2.
– The object previously referenced by c1 is no longer referenced.
– This object is known as garbage.
• Garbage is automatically collected by Java Virtual Machine
(JVM).
• TIP:
– If you know that an object is no longer needed, you can explicitly
assign null to a reference variable for the object.
– The JVM will automatically collect the space if the object is not
referenced by any variable.
1.5 Static Variables, Constants, and Methods
circle1: Circle
Circle 1 radius
radius = 1
radius: double numberOfObjects = 1
numberOfObjects: int
getNumberOfObjects(): int 1 numberOfObjects
getArea(): double
circle1: Circle
Circle 1 radius
radius = 1
radius: double numberOfObjects = 2
numberOfObjects: int
getNumberOfObjects(): int 2 numberOfObjects
getArea(): double
circle2: Circle
5 radius
radius = 5
numberOfObjects = 2
Eg
static int numberOfObjects;
Circle2( ) {
radius = 1.0
numberOfObjects++; }
Circle2(double newRadius) {
radius = newRadius;
numberOfObjects++; }
double getArea( ) {
return radius * radius * Math.PI; }
public Circle3( ) {
numberOfObjects++; }
public class F{
private static double k = 0;
public Circle(){
this(1.0);
}
}
Summary
• The main thing you do to write class definition for the
various problems that will make up the program.
• A class definition encapsulates its object’s data and behavior.
• Once a class has been defined, it serves as a template, or
blueprint, for creating individual objects or instance of the
class.
• A class definition contains two types of elements: variable
and methods.
• Variable – to store the objects information
• Method – to process the information
• To design an object you need to answer five
basic questions:
– What role will the object perform in the program?
– What data of information will it need?
– What actions will it take?
– What interface will it present to other objects?
– What information will it hide from other objects?
Terima Kasih | Thank You