4. ClassesAndObjects1
4. ClassesAndObjects1
class Car {
...description of a car goes here...
}
• Person is a class
Alice and Bob are objects of the Person class.
What does a class have?
• Members of a class:
Attributes (instance variables)
For each instance of the class (object), values of attributes can vary.
Methods
• Person class
Attributes: name, address, phone number
Methods: change address, change phone number
• Alice object
• Name is Alice, address is …
• Bob object
• Name is Bob, address is …
class classname
{ type instance-variable1;
// ...
type instance-variableN;
type methodname1(parameter-list)
{ // body of method
}
// ...
type methodnameN(parameter-list)
{ // body of method
}
}
class Box
{
double width;
double height;
double depth;
}
class Box
{
double width;
double height;
double depth;
}
Box mybox = new Box(); // create a Box object
mybox.width = 100;
class Box
{ double width;
double height;
double depth;
}
class BoxDemo
{ public static void main(String args[])
{ Box mybox = new Box();
double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
} }
class BoxDemo2
{ public static void main(String args[])
{ Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
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);
} }
Declaring Objects
type name(parameter-list)
{
// body of method
}
Adding a Method to the Box Class
class Box
{
double width;
double height;
double depth;
void volume()
{ System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
class BoxDemo3
{ public static void main(String args[])
{ Box mybox1 = new Box();
Box mybox2 = new Box();
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
mybox1.volume();
mybox2.volume();
}}
Returning value
class Box
{
double width;
double height;
double depth;
double volume()
{
return width * height * depth;
}
}
class BoxDemo4
{ public static void main(String args[])
{ Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
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);
} }
// parameterized method.
class Box
{ double width; double height; double depth;
double volume()
{ return width * height * depth; }
vol = mybox1.volume();
System.out.println("Volume is " + vol);
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
class Rect
{
int length, width,area;
}
class prg6
{
public static void main(String args[])
{
Rect r1 = new Rect();
Rect r2 = new Rect();
r1.setData(5,6);
r2.setData(8,9);
System.out.println("area "+res1);
System.out.println("area "+res2);
}
}
Constructor
Constructor
double vol;
vol = mybox1.volume();
System.out.println("Volume is " + vol);
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
Parameterized Constructors
class Box
{
double width; double height; double depth;
}
class prg6
{
public static void main(String args[])
{
Rect r1 = new Rect();
Rect r2 = new Rect();
r1.setData(5,6);
r2.setData(8,9);
System.out.println("area "+res1);
System.out.println("area "+res2);
}
}
class Rect
{
int length, width,area;
Rect(int l,int w)
{
length = l;
width = w;
}
int computeArea()
{
area = length * width;
return area;
}
void disp()
{
System.out.println("area "+area);
}
}
class prg6
{
public static void main(String args[])
{
Rect r1 = new Rect(5,6);
Rect r2 = new Rect(8,9);
// r1.setData(5,6);
// r2.setData(8,9);
System.out.println("area "+res1);
System.out.println("area "+res2);
}
}
A stack class
// This class defines an integer stack that can hold 10 values.
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;
}
// Pop an item from the stack
int pop()
{
if(tos < 0)
{
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}
class TestStack
{ public static void main(String args[])
{
Stack mystack1 = new Stack();
Stack mystack2 = new Stack();
for(int i=0; i<10; i++)
mystack1.push( i );
for(int i=10; i<20; i++)
mystack2.push( i );
System.out.println("Stack in mystack1:");
for(int i=0; i<10; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<10; i++)
System.out.println(mystack2.pop());
}
}
Stack in mystack1:
9
8
7
6
5
4
3
2
1
0
Stack in mystack2:
19
18
17
16
15
14
13
12
11
10
.
OVERLOADING METHODS
class OverloadDemo
{
void test()
{ System.out.println("No parameters");
}
void test(int a)
{ System.out.println("a: " + a);
}
void test(int a, int b)
{ System.out.println("a and b: " + a + " " + b);
}
double test(double a)
{ System.out.println("double a: " + a);
return a*a;
}
}
class Overload
{ public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
double result;
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
// Automatic type conversions apply to overloading.
class OverloadDemo
{ void test()
{ System.out.println("No parameters");
}
void test(int a, int b)
{ System.out.println("a and b: " + a + " " + b);
}
void test(double a)
{ System.out.println("Inside test(double) a: " + a);
}
}
class Overload
{ public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
int i = 88;
ob.test();
ob.test(10, 20);
ob.test(i); // this will invoke test(double)
ob.test(123.2); // this will invoke test(double)
}}
OVERLOADING CONSTRUCTORS
class Box
{ double width; double height; double depth;
Box(double w, double h, double d)
{ width = w; height = h; depth = d; }
Box()
{ width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box }
Box(double len)
{ width = height = depth = len; }
double volume()
{ return width * height * depth; } }
class OverloadCons
{ public static void main(String args[])
{
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol); }}
Using object as a parameter
class Test
{ int a, b;
Test(int i, int j)
{ a = i;
b = j; }