Download - Class Object Constructor Method Overloading
-
7/30/2019 Class Object Constructor Method Overloading
1/37
Classes, Objects andMethods, Defining a Class,
Adding Variables andMethods, Creating Objects
1
-
7/30/2019 Class Object Constructor Method Overloading
2/37
2
The General Form of a Classclass classname
{
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list)
{ // body of method
}
type methodname2(parameter-list)
{
// body of method
}// ...
type methodnameN(parameter-list)
{
// body of method
}
}
-
7/30/2019 Class Object Constructor Method Overloading
3/37
3
A Simple Class
class Box
{
double width;
double height;
double depth;
}
-
7/30/2019 Class Object Constructor Method Overloading
4/37
4
Declaring Objects
Box mybox = new Box();
This statement combines the two steps just described. It can be rewritten like this
to show each step more clearly:
Box mybox; // declare reference to objectmybox = new Box(); // allocate a Box object
-
7/30/2019 Class Object Constructor Method Overloading
5/37
5
Introducing Methods
This is the general form of a method
type name(parameter-list)
{
// body of method}
-
7/30/2019 Class Object Constructor Method Overloading
6/37
6
Adding a Method to the Box Class
class Box
{
double width;
double height;
double depth;// display volume of a box
void volume()
{
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
-
7/30/2019 Class Object Constructor Method Overloading
7/37
7
class BoxDemo3
{
public static void main(String args[])
{
Box mybox1 = new Box();Box mybox2 = new Box();
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// display volume of first box
mybox1.volume();
// display volume of second box
mybox2.volume();
}
}
-
7/30/2019 Class Object Constructor Method Overloading
8/37
8
Output
Volume is 3000.0Volume is 162.0
-
7/30/2019 Class Object Constructor Method Overloading
9/37
9
Returning a Value
// Now, volume() returns the volume of a box.class Box
{
double width;
double height;
double depth;
// compute and return volume
double volume()
{
return width * height * depth;
}
}
-
7/30/2019 Class Object Constructor Method Overloading
10/37
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.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2'sinstance variables */
mybox2.width = 3;
mybox2.height = 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);
}
}
-
7/30/2019 Class Object Constructor Method Overloading
11/37
11
Adding a Method That Takes Parameters
int square(int i){
return i * i;
}
int x, y;
x = square(5); // x equals 25
x = square(9); // x equals 81
y = 2;
x = square(y); // x equals 4
-
7/30/2019 Class Object Constructor Method Overloading
12/37
12
// This program uses a parameterized method.
class Box {
double width;
double height;double depth;
// compute and return volume
double volume() {
return width * height * depth;
}// sets dimensions of box
void setDim(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
}
-
7/30/2019 Class Object Constructor Method Overloading
13/37
13
class BoxDemo5 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();double vol;
// initialize each box
mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);
// get volume of first boxvol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);}
}
-
7/30/2019 Class Object Constructor Method Overloading
14/37
14
Method Overloading// Automatic type conversions apply to overloading.
class OverloadDemo
{
void test()
{
System.out.println("No parameters");
}
// Overload test for two integer parameters.void test(int a, int b)
{
System.out.println("a and b: " + a + " " + b);
}
// overload test for a double parametervoid test(double a)
{
System.out.println("Inside test(double) a: " + a);
}
}
-
7/30/2019 Class Object Constructor Method Overloading
15/37
15
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)}
}
Output:No parametersa and b: 10 20
Inside test(double) a: 88
Inside test(double) a: 123.2
-
7/30/2019 Class Object Constructor Method Overloading
16/37
16
A Closer Look at Argument Passing
call-by-value
call-by-reference
-
7/30/2019 Class Object Constructor Method Overloading
17/37
17
call-by-value// Simple types are passed by value.
class Test
{
void meth(int i, int j)
{
i *= 2; j /= 2;
}
}
class CallByValue{
public static void main(String args[])
{
Test ob = new Test();
int a = 15, b = 20;System.out.println("a and b before call: " +a + " " + b);
ob.meth(a, b);
System.out.println("a and b after call: " +
a + " " + b);
}
}
-
7/30/2019 Class Object Constructor Method Overloading
18/37
18
Output
a and b before call: 15 20a and b after call: 15 20
-
7/30/2019 Class Object Constructor Method Overloading
19/37
19
call-by-reference
// Objects are passed by reference.
class Test{
int a, b;
Test(int i, int j)
{
a = i;b = j;
}
// pass an object
void meth(Test o)
{
o.a *= 2;
o.b /= 2;
}
}
-
7/30/2019 Class Object Constructor Method Overloading
20/37
20
class CallByRef
{
public static void main(String args[])
{Test ob = new Test(15, 20);
System.out.println("ob.a and ob.b before call: " +
ob.a + " " + ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " +ob.a + " " + ob.b);
}
}
Output
ob.a and ob.b before call: 15 20
ob.a and ob.b after call: 30 10
-
7/30/2019 Class Object Constructor Method Overloading
21/37
21
Returning Objects
-
7/30/2019 Class Object Constructor Method Overloading
22/37
22
// Returning an object.
class Test{
int a;
Test(int i)
{
a = i;}
Test incrByTen()
{
Test temp = new Test(a+10);
return temp;}
}
-
7/30/2019 Class Object Constructor Method Overloading
23/37
23
class RetOb
{public static void main(String args[])
{
Test ob1 = new Test(2);
Test ob2;
ob2 = ob1.incrByTen();System.out.println("ob1.a: " + ob1.a);
System.out.println("ob2.a: " + ob2.a);
ob2 = ob2.incrByTen();
System.out.println("ob2.a after second increase: "
+ ob2.a);}
}
-
7/30/2019 Class Object Constructor Method Overloading
24/37
24
Output -
ob1.a: 2
ob2.a: 12
ob2.a after second increase: 22
-
7/30/2019 Class Object Constructor Method Overloading
25/37
Constructors
25
-
7/30/2019 Class Object Constructor Method Overloading
26/37
26
/* Here, Box uses a constructor to initialize the dimensions of a box. */
class Box
{
double width;
double height;double depth;
// This is the constructor for Box.
Box()
{
System.out.println("Constructing Box");width = 10;
height = 10;
depth = 10;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
-
7/30/2019 Class Object Constructor Method Overloading
27/37
27
class BoxDemo6
{
public static void main(String args[]){
// declare, allocate, and initialize Box objects
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// 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);}
}
-
7/30/2019 Class Object Constructor Method Overloading
28/37
28
Output -
Constructing BoxConstructing Box
Volume is 1000.0
Volume is 1000.0
-
7/30/2019 Class Object Constructor Method Overloading
29/37
29
Parameterized Constructor
-
7/30/2019 Class Object Constructor Method Overloading
30/37
30
/* Here, Box uses a parameterized constructor to initialize the
dimensions of a box. */
class Box
{
double width;double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d)
{ width = w;
height = h;
depth = d;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
-
7/30/2019 Class Object Constructor Method Overloading
31/37
31
class BoxDemo7
{
public static void main(String args[])
{// declare, allocate, and initialize Box objects
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;
// get volume of first boxvol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);}
}
-
7/30/2019 Class Object Constructor Method Overloading
32/37
32
Output -
Volume is 3000.0Volume is 162.0
-
7/30/2019 Class Object Constructor Method Overloading
33/37
33
The Default Constructor
-
7/30/2019 Class Object Constructor Method Overloading
34/37
34
Constructor OverloadingIn addition to overloading normal methods, you can also overload
constructor methods.
class Box
{
double width, height, depth;
// constructor used when all dimensions specified
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specifiedBox()
{
width = -1; height = -1; depth = -1;
}
-
7/30/2019 Class Object Constructor Method Overloading
35/37
35
// constructor used when cube is created
Box(double len)
{
width = height = depth = len;
}
// compute and return volume
double volume()
{
return width * height * depth;
}}
-
7/30/2019 Class Object Constructor Method Overloading
36/37
36
class OverloadCons
{
public static void main(String args[])
{
// create boxes using the various constructorsBox mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first boxvol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
}
}
-
7/30/2019 Class Object Constructor Method Overloading
37/37
37
Output -
Volume of mybox1 is 3000.0Volume of mybox2 is -1.0
Volume of mycube is 343.0