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

Java Module 2 Chapter 6

The document provides information about classes in Java including defining a class, creating objects from a class, using methods and constructors, and garbage collection. It defines a Box class with width, height, and depth variables and includes methods to set dimensions, calculate volume, and examples of creating Box objects and calling class methods. It also discusses using the this keyword, parameterized constructors, and the finalize method in Java.

Uploaded by

2022becs190
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java Module 2 Chapter 6

The document provides information about classes in Java including defining a class, creating objects from a class, using methods and constructors, and garbage collection. It defines a Box class with width, height, and depth variables and includes methods to set dimensions, calculate volume, and examples of creating Box objects and calling class methods. It also discusses using the this keyword, parameterized constructors, and the finalize method in Java.

Uploaded by

2022becs190
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Lab Activity/Exercise 12

Write a Java program to add two matrices. Assume


they have same number of rows and columns. Ask the
user to enter N - which is the number of rows (and
columns) in each matrix. Next, ask the user to enter the
elements of the two matrices to be added, one after the
other, row-wise. Finally, compute and print the
resulting matrix with the same number of rows and
columns as before, row-wise.
Class Fundamentals
●A Class is a user-defined data type. It is used to create
any number of objects (limited by memory) of that
type.
●A class is a template for an object, and an object is an
instance of a class.
class is declared by the class keyword.
class className {
type instanceVariable1;
type instanceVariable2;

type instanceVariableN;

type methodName1(parameter-list) {
// body of method
}

type methodNameN(parameter-list) {
// body of method
}
}
A class & its associated test class
class Box {

double width;
double height;
double depth;
}
public class BoxTest {
public static void main(String args[]) {

Box box = new Box();


box.width = 10;
box.height = 20;
box.depth = 15;
double volume;
volume = box.width * box.height * box.depth;
System.out.println("volume is " + volume);
}
}
● Note that we have defined two classes within the same java file BoxTest.java
● The first class is the declaration of the user-defined class called box
● The second class is used to test the functionality of the Box defined earlier.
● Usually, one class i.e, the test class should be made public
Box is a user-defined data type
●A class declaration is just a template, and not an actual
object.
● An object is created by using the new operator
● Box box = new Box(); // create an object
● box is an instance of Box.
Declaring Objects
● obtaining objects of any class is a two-step process.
● declare a variable of the class type
● Box box; // declares a reference to object
● Allocate memory to that Object.
● box = new Box(); // allocate a Box object
● Together we can write it as,
● Box box = new Box();
Lab Activity/Exercise 13
Use the same Box class to define two different objects
called box1 and box2. Assign different widths, heights,
and depths to each object’s member variables, compute
the volume of each object, and print the same.
● class-var = new className( );

● class-var is a variable of the class type being created. The


classname is the name of the class being instantiated.
● The class name followed by parentheses specifies the

constructor for the class.


Assigning Object Reference Variable

● Box b1 = new Box();


● Box b2 = b1;
● Box b1 = new Box();
● Box b2 = b1;
● b1 = null;
Introducing Methods
type name(parameter-list) {
// body of method
return value;
}
Add Method – Improved version
class Box {
double width;
double height;
double depth;

double volume() {
return width * height * depth;
}
}
Lab Activity/Exercise 14
Use the new (or second) version of the Box Class.
Write the new version of the BoxTest class, create a
couple of objects, and use the volume() method in the
print statements to display the volume of each object.
Method Return Type
• The type of data returned by a method must be compatible with
the return type specified by the method. For example, if the return
type of some method is boolean, you should not return an integer.
• The variable receiving the value returned by a method (volume,
in the above case) must also be compatible with the return type
specified for the method.
● Effectively we can write System.out.println("Volume is " +

box1.volume());
Parameterized Methods – Always better
int square() {
return 10 * 10;
}

can be generalized as

int square(int i) {
return i * i;
}
Parameter and argument
square(int i) { // i - parameter in the method definition
}
● square(100) // 100 - argument when you call/invoke
What is wrong with this code?
box1.width = 10;
box1.height = 20;
box1.depth = 15;

● This code leads to trouble for two reasons


● clumsy and error-prone: and easy to forget to set a dimension.

● can’t change the behavior of an exposed instance variable.


To overcome this problem, create another method called
setDimensions() in the class Box

void setDimensions(double w, double h, double d) {


width = w;
height = h;
depth = d;
}
class BoxTest {
public static void main(String args[]) {
Box box1 = new Box();
Box box2 = new Box();
double volume;
box1.setDimensions(10, 20, 15);
box2.setDimensions(3, 6, 9);
volume = box1.volume();
System.out.println("Volume is " + volume);
volume = box2.volume();
System.out.println("Volume is " + volume);
}
}
Constructor
● A constructor initializes an object upon creation
class Box {
double width;
double height;
double depth;

Box() {
System.out.println("Constructing Box");
width = 10;
height = 10;
depth = 10;
}

double volume() {
return width * height * depth;
}
}
class BoxTest {
public static void main(String args[]) {
Box box1 = new Box();
Box box2 = new Box();
double volume;

volume = box1.volume();
System.out.println("Volume is " + volume);

volume = box2.volume();
System.out.println("Volume is " + volume);
}
}
Parameterized Constructors
class Box {
double width;
double height;
double depth;

Box(double w, double h, double d) {


width = w;
height = h;
depth = d;
}

double volume() {
return width * height * depth;
}
}
class BoxTest {
public static void main(String args[]) {
Box box1 = new Box(10, 20, 15);
Box box2 = new Box(3, 6, 9);
double volume;
volume = box1.volume();
System.out.println("Volume is " + volume);
volume = box2.volume();
System.out.println("Volume is " + volume);
}
}
● Box box1 = new Box(10, 20, 15);
●the values 10, 20, and 15 are passed to the Box()
constructor when new creates the object.
●Thus, box1’s copy of width, height, and depth will
contain the values 10, 20, and 15 respectively.
The this Keyword
● this is a keyword.
● Used in any method to refer to the current object.
● It is a reference to an object on which the method is invoked.
● Solves the ambiguity between instance variable and parameter
●Suppose the instance variable and parameter have different
names, then no need of this keyword.
class Student {
int rollNumber;
String name;
float fee;

Student(int r, String n, float f){


rollNumber = r;
name = n;
fee = f;
}
}
class Student {
int rollNumber;
String name;
float fee;

Student(int rollNumber, String name, float fee) {


this.rollNumber = rollNumber;
this.name=name;
this.fee=fee;
}
}
Garbage collection
 Automated process of deleting objects no longer needed or used.
 is the process of destroying unused objects and reclaiming
memory automatically.
• System.gc() is a method in Java that invokes garbage collector
which will delete the unreferenced objects.
• System.gc() calls finalize() method only once for each object
finalize () method
protected void finalize() {
// finalization code here
}
●the keyword protected is a specifier that prevents access
to finalize( ) by code defined outside its class
● finalize() is the method in Java.
●used to perform clean-up processing just before an object is
garbage collected.
● It is used with objects.
●finalize method performs the cleaning with respect to the
object before its destruction.
● finalize method is executed just before the object is destroyed.

You might also like