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

Unit 1-Introduction To CLASSES

Uploaded by

jithentar.cs21
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 views18 pages

Unit 1-Introduction To CLASSES

Uploaded by

jithentar.cs21
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/ 18

Introduction to CLASSES

Prepared by
Syed Akram
On
15/11/2022
The General Form of a Class
class classname { ● Notice that the general form
type instance-variable1; of a class does not specify a
type instance-variable2; main( ) method.
// ... ● You only specify one if that
type instance-variableN; class is the starting point for
type methodname1(parameter-list) { your program.
// body of method
}
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
An Example
class Box {
double width; OUTPUT:
double height; Volume is 3000.0
double depth;
}
class BoxDemo {
public static void main(String args[]) { A Class is a template for a class
Box mybox = new Box(); An Object is an instance of a class.

double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
Two OBJECTS
class BoxDemo2 {
public static void main(String args[]) {
class Box { Box mybox1 = new Box();
double width; Box mybox2 = new Box();
double height; double vol;
double depth; mybox1.width = 10;
}
mybox1.height = 20;
mybox1.depth = 15;
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("Volume is " + vol);
vol = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("Volume is " + vol);
}
}
OUTPUT:
Volume is 3000.0
Volume is 162.0
Creating an Object
Box mybox; ● mybox is an reference to an object of type Box.
mybox = new Box(); ● mybox contains value null.
● Any point to access to mybox results in compile
time error
● Second line actually allocates an actual object &
assigns a reference to mybox.
Assigning Object Reference Variables

Box b1 = new Box();


Box b2 = b1;

❑ b1 has been set to null.


b1 = null;
❑ but b2 still points to the original object.

REMEMBER When you assign one object


reference variable to another object reference
variable, you are not creating a copy of the
object, you are only making a copy of the
reference.
Introducing Methods

type name(parameter-list) {
// body of method
}
class Box { class BoxDemo3 {
double width; public static void main(String args[]) {
double height; Box mybox1 = new Box();
double depth; Box mybox2 = new Box();
mybox1.width = 10;
void volume() { mybox1.height = 20;
System.out.print("Volume is ");
double vol; mybox1.depth = 15;
vol=width * height * depth; mybox2.width = 3;
System.out.println(vol); mybox2.height = 6;
}
} mybox2.depth = 9;
mybox1.volume();
OUTPUT: mybox2.volume();
Volume is 3000.0 }
Volume is 162.0 }
class BoxDemo4 {
class Box {
public static void main(String args[]) {
double width;
Box mybox1 = new Box();
double height;
Box mybox2 = new Box();
double depth;
double vol;
double volume() {
return width * height * depth; mybox1.width = 10;
} mybox1.height = 20;
} mybox1.depth = 15;
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
vol = mybox1.volume();
System.out.println("Volume is " + vol);
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth;
}

void setDim(double w, double h, double d)


{
class BoxDemo5 {
width = w;
height = h; public static void main(String args[]) {
depth = d; Box mybox1 = new Box();
} Box mybox2 = new Box();
} double vol;
mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);
vol = mybox1.volume();
System.out.println("Volume is " + vol);
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
Constructors
A constructor initializes an object immediately upon creation. It has the same name as
the class in which it resides

Once defined, the constructor is automatically called immediately after the object is
created, before the new operator completes.

Constructors has no return type, not even void.


because the implicit return type of a class’ constructor is the class type itself.
class Box {
double width; OUTPUT:
double height; Constructing Box
double depth; Constructing Box
Volume is 1000.0
Box() { Volume is 1000.0
System.out.println("Constructing Box");
width = 10;
height = 10; class BoxDemo6 {
depth = 10; public static void main(String args[]) {
} Box mybox1 = new Box();
double volume() { Box mybox2 = new Box();
return width * height * depth; double vol;
} vol = mybox1.volume();
} System.out.println("Volume is " + vol);
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
Parameterized Constructors
class Box { class BoxDemo7 {
double width; public static void main(String args[]) {
double height; Box mybox1 = new Box(10, 20, 15);
double depth; Box mybox2 = new Box(3, 6, 9);
Box(double w, double h, double d) { double vol;
width = w; vol = mybox1.volume();
height = h; System.out.println("Volume is " + vol);
depth = d; vol = mybox2.volume();
} System.out.println("Volume is " + vol);
double volume() { }
return width * height * depth; }
}
}

Volume is 3000.0
Volume is 162.0
The this Keyword
❖ Sometimes a method will need to refer to the object that invoked it.
❖ To allow this, Java defines the this keyword.
➢ this keyword can be used inside any method to refer to the current object.
➢ That is, this keyword is always a reference to the object on which the
method was invoked.
➢ You can use this keyword anywhere a reference to an object of the current
class’ type is permitted.

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


width = w;
height = h;
Box(double w, double h, double d) { depth = d;
this.width = w; }
this.height = h;
this.depth = d;
}
The finalize( ) Method
an object will need to perform some action when it is destroyed.

For example, if an object is holding some non-Java resource such as a file handle or
character font, then you might want to make sure these resources are freed before an
object is destroyed.

protected void finalize( )


{
// finalization code here
}
A Stack Class
class Stack {
int stck[] = new int[10];
int tos;
Stack() {
tos = -1;
}
void push(int item) {
if(tos==9)
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}
OUTPUT:
class TestStack { Stack in mystack1:
public static void main(String args[]) { 9
Stack mystack1 = new Stack(); 8
Stack mystack2 = new Stack(); 7
for(int i=0; i<10; i++) mystack1.push(i); 6
for(int i=10; i<20; i++) mystack2.push(i); 5
System.out.println("Stack in mystack1:"); 4
for(int i=0; i<10; i++) 3
System.out.println(mystack1.pop()); 2
System.out.println("Stack in mystack2:"); 1
for(int i=0; i<10; i++) 0
System.out.println(mystack2.pop()); Stack in mystack2:
} 19
} 18
17
16
15
14
13
12
11
10
References
• Java The Complete Reference, 7th Edition by
Herbert Schildt

You might also like