Unit 1-Introduction To CLASSES
Unit 1-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
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;
}
Once defined, the constructor is automatically called immediately after the object is
created, before the new operator completes.
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.
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.