0% found this document useful (0 votes)
18 views

03 OOP_Lecture#3

Uploaded by

Abdunnaser Diaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

03 OOP_Lecture#3

Uploaded by

Abdunnaser Diaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

8 ‫ من‬1 ‫الصفحة‬

Java Class and Object Creation

In this lecture, the following topics are covered:


 Class definition in Java
 Object creation in Java
 Reference variables vs Primitive variables

1. Class definition in Java


 A Java class uses variables to define data fields and methods to define
actions.
 Additionally, a class provides methods of a special type, known as
constructors, which are invoked to create a new object.
 A constructor can perform any action, but constructors are designed
to perform initializing actions, such as initializing the data fields of
objects.
 Figure 1 shows a UML class diagram for a class named Circle as well
as the UML notation for objects of the Circle class.

Figure 1. Classes and objects can be represented using UML notation.

Figure 2 shows an example of defining the Circle class in Java.

8 ‫ من‬1 ‫الصفحة‬
8 ‫ من‬2 ‫الصفحة‬

Figure 2. A Java class definition for the Circle class.

2. Object Creation in Java


To illustrate how to create objects from classes in Java, the Circle class is
presented here.

Example: The Circle class (Circle.java) and its test (CircleTest.java)


 Listing 1 is a Java program that defines the Circle class (Circle.java).
 Listing 2 is Java program (CircleTest.java) used to test the Circle class by
creating objects. Such a program is often referred to as a client of the
class.
o This program constructs three circle objects with radius 1, 25, and
125 and displays the radius and area of each of the three circles.
o It then changes the radius of the second object to 3 and displays its
new radius and area.
o The class contains the main method that creates the three objects.

8 ‫ من‬2 ‫الصفحة‬
8 ‫ من‬3 ‫الصفحة‬

o The presence of the main method in the class makes the class
executable.
 The new operator is used to create an object from the class using the
appropriate constructor:
o new Circle () creates an object with radius 1, new Circle (25)
creates an object with radius 25, and new Circle (125) creates an
object with radius 125.
o These three objects (referenced by circle1, circle2, and circle3)
have different data but the same methods. Therefore, you can
compute their respective areas by using the getArea () method.
o The data fields can be accessed via the reference of the object
using circle1.radius, circle2.radius, and circle3.radius,
respectively.

Class.java1 Listing

8 ‫ من‬3 ‫الصفحة‬
8 ‫ من‬4 ‫الصفحة‬

Listing2 . CircleTest.java

Once the CircleTest.java is run, the following outputs are shown:

The alternative to have a separate class (CircleTest.java) for testing the


Circle class is to include the main method in the Circle class and create
objects in this method.
3. 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
characteristics:
o A constructor must have the same name as the class itself.

8 ‫ من‬4 ‫الصفحة‬
8 ‫ من‬5 ‫الصفحة‬

o Constructors do not have a return type—not even void.


o Constructors are invoked using the new operator when an
object is created.
 Constructors play the role of initializing objects.
 Like regular methods, constructors can be overloaded (i.e., multiple
constructors can have the same name but different signatures),
making it easy to construct objects with different initial data values.
 It is a common mistake to put the void keyword in front of a
constructor. For example,

In this case, Circle () is a method, not a constructor. Constructors are used to


construct objects. To construct an object from a class, invoke a constructor
of the class using the new operator, as follows:

For example, new Circle () creates an object of the Circle class using the first
constructor defined in the Circle class, and new Circle (25) creates an object
using the second constructor defined in the Circle class.
Aclass normally provides a constructor without arguments (e.g., Circle ()).
Such a constructor is referred to as a no-arg or no-argument constructor.
A class may be defined without constructors. In this case, a public no-arg
constructor with an empty body is implicitly defined in the class. This
constructor, called a default constructor, is provided automatically only if no
constructors are explicitly defined in the class

4. Accessing Objects via Reference Variables


An object’s data and methods can be accessed through the dot (.) operator via the
object’s reference variable.
Newly created objects are allocated in the memory. They can be accessed via
reference variables

8 ‫ من‬5 ‫الصفحة‬
8 ‫ من‬6 ‫الصفحة‬

Reference Variables and Reference Types


Objects are accessed via the object’s reference variables, which contain
references to the objects. Such variables are declared using the following
syntax:

A class is essentially a programmer-defined type. A class is a reference type,


which means that a variable of the class type can reference an instance of the
class. The following statement declares the variable myCircle to be of the
Circle type:

The variable myCircle can reference a Circle object. The next statement
creates an object and assigns its reference to myCircle:

You can write a single statement that combines the declaration of an object
reference variable, the creation of an object, and the assigning of an object
reference to the variable with the following syntax:

Here is an example:

The variable myCircle holds a reference to a Circle object.


An object reference variable that appears to hold an object actually contains
a reference to that object
Accessing an Object’s Data and Methods
 In OOP terminology, an object’s member refers to its data fields 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:
o objectRefVar.dataField references a data field in the object.

8 ‫ من‬6 ‫الصفحة‬
8 ‫ من‬7 ‫الصفحة‬

o objectRefVar.method(arguments) invokes a method on the


object.
 For example, myCircle.radius references the radius in myCircle, and
myCircle.getArea() invokes the getArea method on myCircle. Methods
are invoked as operations on objects.
 The data field radius is referred to as an instance variable, because it is
dependent on a specific instance. For the same reason, the method
getArea is referred to as an instance method, because you can invoke it
only on a specific instance.
 The object on which an instance method is invoked is called a calling
object.
Differences between Variables of Primitive Types and Reference Types
Every variable represents a memory location that holds a value. When you
declare a variable, you are telling the compiler what type of value the variable
can hold. For a variable of a primitive type, the value is of the primitive type.
For a variable of a reference type, the value is a reference to where an object
is located. For example, as shown in Figure 3, the value of int variable i is int
value 1, and the value of Circle object c holds a reference to where the
contents of the Circle object are stored in memory.

Figure 3. A variable of a primitive type holds a value of the primitive type, and a
variable of a reference type holds a reference to where an object is stored in memory

When you assign one variable to another, the other variable is set to the same
value. For a variable of a primitive type, the real value of one variable is
assigned to the other variable. For a variable of a reference type, the
reference of one variable is assigned to the other variable. As shown in
8 ‫ من‬7 ‫الصفحة‬
8 ‫ من‬8 ‫الصفحة‬

Figure 4, the assignment statement i = j copies the contents of j into I for


primitive variables.

Figure 4. Primitive variable j is copied to variable i.

As shown in Figure 5, the assignment statement c1 = c2 copies the reference


of c2 into c1 for reference variables. After the assignment, variables c1 and
c2 refer to the same object.

Figure 5. Reference variable c2 is copied to variable c1.

8 ‫ من‬8 ‫الصفحة‬

You might also like