Professional Documents
Culture Documents
FALLSEM2022-23 BCSE103E ETH VL2022230104615 Reference Material I 08-08-2022 Chapter 3
FALLSEM2022-23 BCSE103E ETH VL2022230104615 Reference Material I 08-08-2022 Chapter 3
Keerthika C
EMP ID:18974
SCOPE
1
OO Programming Concepts
3
Objects
Class Name: Circle A class template
Data Fields:
radius is _______
Methods:
getArea
An object has both a state and behavior. The state defines the object,
and the behavior defines what the object does.
4
Classes
Classes are constructs that define objects of the same type. A Java
class uses variables to define data fields and methods to define
behaviors. Additionally, a class provides a special type of methods,
known as constructors, which are invoked to construct objects from
the class.
5
Classes
class Circle {
/** The radius of this circle */
double radius = 1.0; Data field
7
Unified Modeling Language (UML) Class
Diagram
9
Example: SimpleCircle class
10
Example: SimpleCircle class
• The program contains two classes. The first of these,
TestSimpleCircle, is the main class. Its sole purpose is to test
the second class, SimpleCircle. Such a program that uses the
class is often referred to as a client of the class. When you run
the program, the Java runtime system invokes the main
method in the main class.
• You can put the two classes into one file, but only one class in
the file can be a public class. Furthermore, the public class
must have the same name as the file name. Therefore, the file
name is TestSimpleCircle.java, since TestSimpleCircle is
public.
• Each class in the source code is compiled into a .class file.
When you compile TestSimpleCircle.java, two class files
TestSimpleCircle.class and SimpleCircle.class are generated,
11
Combine Two Classes into One
12
C
o
m
bi
ne
T
w
o
Cl
as
se
s
int
o
O
ne
13
Example: Defining Classes and Creating Objects
TV
channel: int The current channel (1 to 120) of this TV.
volumeLevel: int The current volume level (1 to 7) of this TV.
on: boolean Indicates whether this TV is on/off.
15
Example: Defining Classes and Creating Objects
16
Visibility Modifiers and Accessor/Mutator
Methods
• You can use the public visibility modifier for classes, methods,
and data fields to denote that they can be accessed from any
other classes.
• If no visibility modifier is used, then by default the classes,
methods, and data fields are accessible by any class in the same
package. This is known as package-private or package-access.
• Packages can be used to organize classes. To do so, you need to
add the following line as the first statement in the program:
package packageName;
• If a class is defined without the package statement, it is said to be
placed in the default package.
17
Access Specifiers
public
• The class, data, or method is visible to any class in any
package.
private
• The data or methods can be accessed only by the declaring
class.
• Public getter (Accessor) and setter (Mutator) methods are used
to read and modify private properties.
18
Examples
21
NOTE
22
NOTE
Outside
Access Inside Outside
Inside Class package
Specifier Package package
subclass
Private Yes No No No
Default Yes Yes No No
Protected Yes Yes Yes No
Public Yes Yes Yes Yes
23
Non-access specifiers
7 Non-access modifiers/ specifiers :
1. Static
2. Final
3. Abstract
4. Synchronized
5. Transient
6. Strictfp
7. Native
24
Non-access specifiers
Final non-access modifier:
This modifier can be applied with:
• Class
• Method
• Instance Variable
• Local Variable
• Method arguments
Final:
final class Honda{
public final void myFun1(){
System.out.println("Honda Class");
}}
class Bike extends Honda{
public void myFun1(){
System.out.println("Bike Class");
}
25
}
Non-access specifiers
Abstract:
Abstract keyword is used to declare a class as partially
implemented means an object cannot be created directly from
that class. Any subclass needs to be either implement all the
methods of the abstract class, or it should also need to be an
abstract class.
• Abstract class
• Abstract method
Synchronized:
This keyword helps prevent the access of one method by
multiple threads simultaneously, thus synchronizing the flow
of a program and bringing out the desired results using the
multithreading feature.
26
Non-access specifiers
Synchronized:
Example:
class Person1 {
public synchronized void sendFun(String txt) {
System.out.println("Sending message\t" + txt );
try {
Thread.sleep(1000);
}
catch (Exception e) {
System.out.println("Thread interrupted.");
}
System.out.println("\n" + txt + "Sent");
}
}
27
Non-access specifiers
28
Non-access specifiers
Static:
This variable is used for memory management and the first
thing being referenced while loading a class. These members
are treated on a class level; thus, they cannot be called using an
object; instead, the name of the class is used to refer to them.
Can be used in:
• Variables
• Classes
• Methods
• Blocks
30
Non-access specifiers
Static:
Example:
public class Demo {
// static variable
static int x = 10;
static int y;
//static class
public static class DemoInnerClass{
static int z=10;
}
// static block
static {
System.out.println("Static block initialized.");
y = x + 4;
}
31
Non-access specifiers
//static method
public static void main(String[] args)
{
System.out.println("from main");
System.out.println("Value of x : "+x);
System.out.println("Value of y : "+y);
System.out.println("Value of z : "+DemoInnerClass.z);
}
}
32
Non-access specifiers
Native:
The native keyword is used only with the methods to indicate
that the particular method is written in platform -dependent.
These are used to improve the system’s performance, and the
existing legacy code can be easily reused.
Note: Static, as well as abstract methods, cannot be declared as
native.
33
Non-access specifiers
Native:
Example:
import java.io.*;
class Main
{
public native void printMethod ();
static{
System.loadLibrary ("LibraryName");
}
public static void main (String[] args)
{
Main obj = new Main ();
obj.printMethod ();
}
} 34
Non-access specifiers
Strictfp class/method:
This keyword is used to ensure that results from an operation on
floating-point numbers brings out the same results on every platform.
This keyword can not be used with abstract methods, variables or
constructors as these need not contain operations.
Example:
public class HelloWorld {
public strictfp double calSum() {
double n1 = 10e+07;
double n2 = 9e+08;
return (n1+n2);
}
public static strictfp void main(String[] args) {
HelloWorld t = new HelloWorld ();
System.out.println("Result is -" + t.calSum());
}} 35
Non-access specifiers
Transient:
While transferring the data from one end to another over a network,
it must be serialised for successful receiving of data, which means
convert to byte stream before sending and converting it back at
receiving end. To tell JVM about the members who need not undergo
serialization instead of being lost during transfer, a transient modifier
comes into the picture.
Syntax:
private transient member1;
Non-access modifiers are the type of modifiers that tell JVM about
the behavior of classes, methods, or variables defined and prepared
accordingly. It also helps in synchronizing the flow as well as
displaying similar results from operations being performed
irrespective of the platform used for execution.
36
Declaring Object Reference Variables
37
Declaring/Creating Objects
in a Single Step
Example:
Circle myCircle = new Circle();
38
Accessing Object’s Members
39
Trace Code
Declare myCircle
yourCircle.radius = 100;
40
Trace Code
Create a circle
41
Trace Code
42
Trace Code
Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();
radius: 5.0
yourCircle no value
Declare yourCircle
43
Trace Code
Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();
radius: 5.0
yourCircle no value
: Circle
Create a new radius: 1.0
Circle object
44
Trace Code
Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();
radius: 5.0
radius: 1.0
45
Trace Code
Circle myCircle = new Circle(5.0);
myCircle reference value
Circle yourCircle = new Circle();
radius: 5.0
: Circle
Change radius in
radius: 100.0
yourCircle
46
Caution
47
Caution
48
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.
49
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.
50
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.
52
Differences between Variables of
Primitive Data Types and Object Types
53
Copying Variables of Primitive Data Types and Object Types
54
Garbage Collection
55
The Date Class
Java provides a system-independent encapsulation of date and time in
the java.util.Date class. You can use the Date class to create an instance
for the current date and time and use its toString method to return the
date and time as a string.
56
The Date Class Example
57
The Random Class
You have used Math.random() to obtain a random double value
between 0.0 and 1.0 (excluding 1.0). A more useful random number
generator is provided in the java.util.Random class.
java.util.Random
+Random() Constructs a Random object with the current time as its seed.
+Random(seed: long) Constructs a Random object with a specified seed.
+nextInt(): int Returns a random int value.
+nextInt(n: int): int Returns a random int value between 0 and n (exclusive).
+nextLong(): long Returns a random long value.
+nextDouble(): double Returns a random double value between 0.0 and 1.0 (exclusive).
+nextFloat(): float Returns a random float value between 0.0F and 1.0F (exclusive).
+nextBoolean(): boolean Returns a random boolean value.
58
The Random Class Example
If two Random objects have the same seed, they will generate identical
sequences of numbers. For example, the following code creates two
Random objects with the same seed 3.
59
The Point2D Class
Java API has a conveninent Point2D class in the javafx.geometry
package for representing a point in a two-dimensional plane.
60
The Point2D Class
61
Passing Objects to Methods
62
Passing Objects to Methods
63
Passing a primitive type value and a reference value
64
Passing a primitive type value and a reference value
65
Passing Objects to Methods
66