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
[Link] = 10;
[Link] = 20;
[Link] = 15;
[Link] = 20;
[Link] =30;
[Link] = 45;
// compute volume of box 6
vol = [Link] * [Link] * [Link];//(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() {
[Link]("Volume is ");
[Link](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
[Link] = 10; myobj1 myobj2
[Link] = 20;
[Link] = 15;
/* assign different values to mybox2's
instance variables */
[Link] = 3;
[Link] = 6;
[Link] = 9;
// display volume of first box
[Link](); //function is going to get called
// display volume of second box
[Link]();
}
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
[Link] = 10;
[Link] = 20;
[Link] = 15;
/* assign different values to mybox2's
instance variables */
[Link] = 3;
[Link] = 6;
[Link] = 9;
// get volume of first box
vol= [Link]();
[Link]("Volume is " + vol);
// get volume of second box
vol = [Link]();
[Link]("Volume is " + vol); 11
}
}