OOP1 - Unit 4 (Amiraj) VisionPapers - in
OOP1 - Unit 4 (Amiraj) VisionPapers - in
❖ It is a basic unit of Object Oriented Programming and represents the real life
entities. A typical Java program creates many objects, which as you know,
EXAMPLE:
Data Name
Data Field
Methods
Class Template
CONSTRUCTORS
❖ In Java, every class has its constructor that is invoked automatically when an object of
method.
❖ A Java method and Java constructor can be differentiated by its name and return type.
A constructor has the same name as that of class and it does not return any value. For
example,
❖
ACCESSING OBJECTS VIA REFERENCE
VARIABLE
➢ boolean after(Date date) : Tests if current date is after the given date.
➢ boolean before(Date date) : Tests if current date is before the given
date.
➢ int compareTo(Date date) : Compares current date with given date.
Returns 0 if the argument Date is equal to the Date; a value less than 0 if
the Date is before the Date argument; and a value greater than 0 if the
Date is after the Date argument.
➢ long getTime() : Returns the number of milliseconds since January 1,
1970, 00:00:00 GMT represented by this Date object.
➢ void setTime(long time) : Changes the current date and time to given
time.
RANDOM CLASS
❖ Random class is used to generate pseudo-random numbers in java. An instance of this
class is thread-safe. The instance of this class is however cryptographically insecure. This
class provides various method calls to generate different random data types such as float,
double, int.
❖ Constructors:
extends Object
implements Serializable
1)java.util.Random.doubles(): Returns an effectively unlimited stream of pseudo
random double values, each between zero (inclusive) and one (exclusive)
Syntax:
public DoubleStream doubles()
Returns:
Returns:
Returns:
4)next(int bits): java.util.Random.next(int bits) Generates the next pseudo random number
Syntax:
protected int next(int bits)
Parameters:
Returns:
Returns:
Parameters:
❖ In Java, if we want to access class members, we must first create an instance of the
class. But there will be situations where we want to access class members without
❖ In those situations, we can use the static keyword in Java. If we want to access class
members without creating an instance of the class, we need to declare the class
members static.
❖ The Math class in Java has almost all of its members static. So, we can access its
a method, the situation changes dramatically, because objects are passed by what is effectively
call-by-reference. Java does this interesting thing that’s sort of a hybrid between pass-by-value
and pass-by-reference. Basically, a parameter cannot be changed by the function, but the
function can ask the parameter to change itself via calling some method within it.
➢ While creating a variable of a class type, we only create a reference to an object. Thus, when we pass this
reference to a method, the parameter that receives it will refer to the same object as that referred to by the
argument.
➢ This effectively means that objects act as if they are passed to methods by use of call-by-reference.
➢ Changes to the object inside the method do reflect in the object used as an argument.
ARRAY OF OBJECTS
❖ JAVA ARRAY OF OBJECT, as defined by its name, stores an
array of objects. Unlike a traditional array that store values like
string, integer, Boolean, etc an array of objects stores
OBJECTS. The array elements store the location of the
reference variables of the object.
IMMUTABLE OBJECTS AND CLASSES