F CSC125 - Ch9
F CSC125 - Ch9
Object Oriented
Programming
Chapter 9
Dr Khulood Alkhateeb
Objects and
Classes
Introduction
■ turnOn turnOff
■ Set channel
■ Set volume
■ Etc
Activity
● First create a UML diagram for the circle class then write the code
Activity
If you run the project nothing happens -> you need to create the objects
For that you need to use main in your main class(the class that contains main)
Creates a circle
object that uses
the default value
of radius set
inside of the
Circle Class
Activity
Now create a new circle object with a different value for radius:
● constructors can be overloaded (just like what we did with max method)
Activity
● Newly created objects are allocated in the memory. They can be accessed
via reference variables
variable myCircle
can reference a
Circle object
Object Access via Reference Variables
● Example:
Accessing an Object’s Data and Methods
● An object’s data fields and methods can be accessed through the dot (.)
operator via the object’s reference variable
● dot operator = object member access operator
● objectRefVar.dataField:
○ references a data field in the object and is referred to as an instance variable
● objectRefVar.method(arguments):
○ invokes a method on the object and is referred to as an instance method
● The object on which an instance method is invoked is called a calling
object
Accessing an Object’s Data and Methods
The answer is no
Reference Data Fields and the null Value
● If a data field of a reference type does not reference any object, the data
field holds a special Java value, null
● null is a literal just like true and false
● 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
● Note: Java assigns no default value to a local variable inside a regular
method
Activity
Then create a Student object without assigning values and print them, see
what you get.
Differences between Variables of Primitive Types
and Reference Types
What
happens to
this?
Differences between Variables of Primitive Types
and Reference Types
● Note: 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 using its garbage collection.
TestCircle example
Static Variables, Constants, And Methods
● The keyword public is a visibility modifier for classes, methods, and data fields
● It denotes that they can be accessed from any other classes.
● Visibility modifiers can be used to specify the visibility of a class and its
members.
● A visibility modifier specifies how data fields and methods in a class can be
accessed from outside 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 -> aka package-private
or package-access
● If a class is defined without the package statement, it is said to be placed in the
default package
Visibility Modifiers
● If a class is not defined as public, it can be accessed only within the same
package
Visibility Modifiers
Note:
● if you want to prohibit the user from creating an instance of a class, use a
private constructor.
● Example:
○ To prevent the user from creating objects from the Math class, the constructor in
java.lang.Math is defined as follows
Data Field Encapsulation
● Is it okay that the data fields radius and numberOfObjects in the Circle
class can be modified directly?
● Not good practice. Why?
○ Data may be tampered with. Example: numberOfObjects is to count the number of objects
created, but it may be mistakenly set to an arbitrary value that is not the real number of
objects created.
○ The class becomes difficult to maintain and vulnerable to bugs. Example: Suppose that
you want to ensure that the radius is non-negative. how when it can be modified directly
(e.g., c1.radius = –5)
● Solution:
○ Data Field Encapsulation
Data Field Encapsulation
Change the Circle class create before to make the data-field private and create
their associated accessor and mutator methods
Activity
Data Field Encapsulation
● What if main was inside Circle class, will main be able to access a private
member? Try it out
Passing objects to methods
Add a method to the TestCircle class to print information about a created Circle
object. For example, print its radius and its area
Immutable objects and classes
● You can create an object whose contents cannot be changed once the
object has been created.
● We call such an object as immutable object and its class as immutable
class
● If a class is immutable, then all its data fields must be private and it cannot
contain public setter methods for any data fields
Activity
Is this immutable?
y=0
The this reference
(a) uses this to reference the object’s radius and invokes its getArea() method explicitly, often not needed and
often omitted to make the code shorter
The this reference
● It is a good practice to use the data field as the parameter name in a setter
method or a constructor to make the code easy to read and to avoid
creating unnecessary names
● You can separate the Circle class from the main class TestCircle. If you
want to put the class in a separate file you need The constructor and
methods in the class to be defined public so they can be accessed from
other classes.
● you can combine the two classes in the preceding TestCircle example into
one:
○ Put the main in the circle class itself
Activity