0% found this document useful (0 votes)
9 views46 pages

OOP1 - Unit 4 (Amiraj) VisionPapers - in

Chapter 4 covers the fundamentals of objects and classes in Object-Oriented Programming (OOP) with a focus on Java. It explains how to define classes, create objects, use constructors, and access object members through reference variables. Additionally, it discusses visibility modifiers, static methods, passing objects to methods, and the concept of immutable objects.

Uploaded by

Arya Desai
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)
9 views46 pages

OOP1 - Unit 4 (Amiraj) VisionPapers - in

Chapter 4 covers the fundamentals of objects and classes in Object-Oriented Programming (OOP) with a focus on Java. It explains how to define classes, create objects, use constructors, and access object members through reference variables. Additionally, it discusses visibility modifiers, static methods, passing objects to methods, and the concept of immutable objects.

Uploaded by

Arya Desai
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/ 46

CHAPTER 4

OBJECTS AND CLASSES

SUBJECT:OOP-I PREPARED BY:


CODE:3140705 ASST.PROF.NENSI KANSAGARA
(CSE DEPARTMENT,ACET)
DEFINING CLASSES FOR OBJECTS
CLASS:

❖ A class is a user defined blueprint or

prototype from which objects are

created. It represents the set of

properties or methods that are

common to all objects of one type.

In general, class declarations can

include these components, in order:


➢ Modifiers : A class can be public or has default access (Refer this
for details).
➢ Class name: The name should begin with a initial letter (capitalized
by convention).
➢ Superclass(if any): The name of the class’s parent (superclass), if
any, preceded by the keyword extends. A class can only extend
(subclass) one parent.
➢ Interfaces(if any): A comma-separated list of interfaces
implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one interface.
➢ Body: The class body surrounded by braces, { }.
HOW TO DEFINE CLASS?
OBJECTS

❖ 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,

interact by invoking methods. An object consists of :

➢ State : It is represented by attributes of an object. It also reflects the


properties of an object.
➢ Behavior : It is represented by methods of an object. It also reflects the
response of an object with other objects.
➢ Identity : It gives a unique name to an object and enables one object to
interact with other objects.
❖ Example of an object : dog
HOW TO DEFINE OBJECT?

EXAMPLE:
Data Name

Data Field

Methods

Class Template
CONSTRUCTORS
❖ In Java, every class has its constructor that is invoked automatically when an object of

the class is created. A constructor is similar to a method but in actual, it is not a

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

❖ The only way you can access an object is through a


reference variable. A reference variable is declared to be
of a specific type and that type can never be changed.
Reference variables can be declared as static variables,
instance variables, method parameters, or local variables.
❖ Reference Variable Syntax:
➢ Class_name Object_Refrence_Varaible =new
Class_name();
❖ Example
➢ Student S= new Student();
❖ Syntax of accessing Object’s member:
➢ Object_variable_name.object_member;
CLASSES FROM JAVA LIBRARY
❖ The type constraints are used to control where the java library classes can
be replaced with routine versions without affecting type perfection of
programs.
❖ Static analysis is then used to control those applicants for which unused
library functionality and synchronization can be removed safely from the
allocated types.
❖ The profile data is collected about the usage features of the customization
candidates to determine where the allocation of custom library classes is
likely to be cost-effective.
❖ To base on the static analysis results and the profiling information the
custom library classes are automatically generated from a template.
❖ The bytecode of the client application is rewritten to use the generated
custom classes. This bytecode rewriting is completely see-through to the
programmer.
DATE CLASS
❖ The Date class of java. util package implements Serializable, Cloneable and
Comparable interface. It provides constructors and methods to deal with date and
time with java. Date() : Creates date object representing current date and time.
❖ Constructors

➢ Date() : Creates date object representing current date and time.


➢ Date(long milliseconds) : Creates a date object for the given milliseconds
since January 1, 1970, 00:00:00 GMT.
➢ Date(int year, int month, int date)
➢ Date(int year, int month, int date, int hrs, int min)
➢ Date(int year, int month, int date, int hrs, int min, int sec)
➢ Date(String s)
❖ Important Methods

➢ 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:

➢ Random(): Creates a new random number generator


➢ Random(long seed): Creates a new random number generator using a single long
seed
DECLARATION

public class Random

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:

a stream of pseudorandom double values

2)java.util.Random.ints(): Returns an effectively unlimited stream of pseudo random int


values
Syntax:
public IntStream ints()

Returns:

a stream of pseudorandom int values


3)java.util.Random.longs(): Returns an effectively unlimited stream of pseudo random long
values
Syntax:
public LongStream longs()

Returns:

a stream of pseudorandom long values

4)next(int bits): java.util.Random.next(int bits) Generates the next pseudo random number
Syntax:
protected int next(int bits)

Parameters:

bits - random bits

Returns:

the next pseudo random value


5)java.util.Random.nextBoolean(): Returns the next pseudo random, uniformly distributed boolean
value from this random number generator’s sequence
Syntax:
public boolean nextBoolean()

Returns:

the next pseudorandom, uniformly distributed boolean value

1. from this random number generator's sequence

6)java.util.Random.nextBytes(byte[] bytes) :Generates random bytes and places them into a


user-supplied byte array
Syntax:
public void nextBytes(byte[] bytes)

Parameters:

bytes - the byte array to fill with random bytes


STATIC VARIABLE,METHODS AND
CONSTANTS

❖ 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

creating any variables.

❖ 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

members without creating instances of the Math class. For example,


STATIC METHOD
❖ Static methods are also called class
methods. It is because a static
method belongs to the class rather
than the object of a class.
❖ And we can invoke static methods
directly using the class name. For
example,
VISIBILITY MODIFIERS
❖ In Java, access modifiers are used to set the accessibility (visibility) of classes,
interfaces, variables, methods, constructors, data members, and the setter
methods. For example,
❖ In the above example, we have declared 2 methods: method1() and method2().
Here,
➢ method1 is public - This means it can be accessed by other classes.
➢ method2 is private - This means it can not be accessed by other classes.
❖ Note the keyword public and private. These are access modifiers in Java. They are
also known as visibility modifiers.
PASSING OBJECTS TO METHODS
❖ When we pass a primitive type to a method, it is passed by value. But when we pass an object to

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

❖ Immutable class means that once an object is created, we cannot


change its content. In Java, all the wrapper classes (like Integer,
Boolean, Byte, Short) and String class is immutable. We can create
our own immutable class as well. ... The class must be declared as
final (So that child classes can't be created)
SCOPE OF VARIABLE
❖ By default, a variable has default access. Default access modifier means we do
not explicitly declare an access modifier for a class, field, method, etc.
❖ A variable or method declared without any access control modifier is available
to any other class in the same package. The fields in an interface are implicitly
public static final and the methods in an interface are by default public.
❖ Java provides a number of access modifiers to set access levels for classes,
variables, methods, and constructors. The four access levels are -
➢ Visible to the package, the default. No modifiers are needed.
➢ Visible to the class only (private).
➢ Visible to the world (public).
➢ Visible to the package and all subclasses (protected).
THIS REFERENCE
In Java, this
keyword is used
to refer to the
current object
inside a method
or a constructor.
For example,

You might also like