Classes and Objects in Java
Basics of Classes in Java
By. Ms Gargi Mukherjee
1
Contents
• Introduction to classes and objects in Java.
• Understand how some of the OO concepts learnt so far are supported in
Java.
• Understand important features in Java classes.
2
Introduction
• Java is a true Object Oriented language and therefore
the underlying structure of all Java programs is
classes.
• Anything we wish to represent in Java must be
encapsulated in a class that defines the “state” and
“behavior” of the basic program components known
as objects.
• Classes create objects and objects use methods to
communicate between them.
3
Classes
• A class is a user defined abstract datatype.
class Box { mybox
double width; Widwidth=
10
double length; ,length=15,
Depth=12
double depth;
} to create the object syntax
Box mybox=new Box( );
4
Program to demonstrate working of a
class
5
class Box {
double width; //declared variables
double length;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[ ]) {
Box mybox = new Box(); //an object mybox of the class box is created,new is
the keyword thru which it is created.
Box mybox1=new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.length = 20;
mybox.depth = 15;
mybox1.width = 20;
mybox1.length =30;
mybox1.depth = 45;
// compute volume of box 6
vol = mybox.width * mybox.length * mybox.depth;//(10*20*15)
Adding Methods(functions)
• A class with only data fields has no life. Objects
created by such a class cannot respond to any
messages.
• Methods are declared inside the body of the class but
immediately after the declaration of data fields.
• The general form of a method declaration is:
returntype MethodName (parameter-list)
{
Method-body;
}
7
Adding Methods to Class Circle
class Box {
double width;
double length;
double depth;
// display volume of a box
void volume() {
System.out.print("Volume is ");
System.out.println(width * length * depth);
}
} Method Body
8
class BoxDemo3 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box(); W=10,l=20,
3,6,9
d=15
// assign values to mybox1's instance variables
mybox1.width = 10; myobj1 myobj2
mybox1.length = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.length = 6;
mybox2.depth = 9;
// display volume of first box
mybox1.volume(); //function is going to get called
// display volume of second box
mybox2.volume();
}
9
}
Now, volume()
class Box {
returns the volume of a box.
double width;
double length;
double depth;
// compute and return volume
double volume() {
return width * length * depth;
}
}
10
class BoxDemo4 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.length = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.length = 6;
mybox2.depth = 9;
// get volume of first box
vol= mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol); 11
}
}