Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

Object Oriented Paradigm

Lecture # 7
Outline
 Overloading Methods
 Overloading Constructors
 Using Objects as Parameters
 Construct Copy of an object (Copy Constructor & Clone)
 Argument Passing
 Returning Objects
 Access Control
 static
 final
2
Overloading Methods
 It is possible to define two or more methods
within the same class that share the same
name, as long as their parameter
declarations are different.
 Such methods are called overloaded, and
the process is referred to as method
overloading.
 Method overloading is one of the ways of
implementing polymorphism.
3
Overloading Methods
 When an overloaded method is invoked, Java uses the
type and/or number of arguments as its guide to
determine which version of the overloaded method to
actually call.
 If an exact type-match is not found, Java will employ its
automatic type conversions.
 While overloaded methods may have different return
types, the return type alone is insufficient to
distinguish two versions of a method.

4
Overloading Methods
// Demonstrate method // Overload test for two
overloading. integer parameters.
class OverloadDemo { void test(int a, int b) {
void test() { System.out.println("a and
System.out.println("No b: " + a + " " + b);
parameters"); }
} // overload test for a double
// Overload test for one parameter
integer parameter. double test(double a) {
void test(int a) { System.out.println("double
System.out.println("a: " + a: " + a);
a); return a*a;
} }
}
5
Overloading Methods
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.2);
System.out.println("Result of ob.test(123.2): " + result);
} No parameters
} a: 10
a and b: 10 20
double a: 123.25
6
Result of ob.test(123.25): 15190.5625
Overloading Constructors
/* Here, Box defines three constructors to initialize
the dimensions of a box various ways.
*/
class Box {
double width;
double height;
double depth;
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}

7
Overloading Constructors
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}

8
Overloading Constructors
class OverloadCons {
public static void main(String args[]) {
// create boxes using the various constructors
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);
}
}

Volume of mybox1 is 3000.0


Volume of mybox2 is -1.0
Volume of mycube is 343.0
9
Using Objects as Parameters
// Objects may be passed to methods.
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// return true if o is equal to the invoking object
boolean equals(Test o) {
if(o.a == a && o.b == b) return true;
else return false;
}
}

10
Using Objects as Parameters
class PassOb {
public static void main(String args[]) {
Test ob1 = new Test(100, 22);
Test ob2 = new Test(100, 22);
Test ob3 = new Test(-1, -1);

System.out.println("ob1 == ob2: " + ob1.equals(ob2));

System.out.println("ob1 == ob3: " + ob1.equals(ob3));


}
}
ob1 == ob2: true
ob1 == ob3: 11
false
Copy Constructor
 Sometimes, we required to create an exact copy of an
existing object of the class.
 There is also a condition, if we have made any changes
in the copy it should not reflect in the original one and
vice-versa.
 For such cases, Java provides the concept of a copy
constructor.

12
Copy Constructor
 In Java, a copy constructor is a special type of
constructor that creates an object using another object
of the same Java class.
 It returns a duplicate copy of an existing object of the
class.

13
Use of Copy Constructor
We can use the copy constructor if we want to:
– Create a copy of an object that has multiple fields.

– Generate a deep copy of the heavy objects.

– Avoid the use of the Object.clone() method.

14
Advantages of Copy Constructor
Advantages of copy constructor are as follows:
– If a field declared as final, the copy constructor can change it.

– There is no need for typecasting.

– Its use is easier if an object has several fields.

– Addition of field to the class is easy because of it. We need to


change only in the copy constructor.

15
Construct copy of an Object
 We may want to construct a new object so that
it is initially the same as some existing object.

 To do this, we must define a constructor that


takes an object of its class as a parameter.

16
Construct copy of an Object
// Here, Box allows one object to initialize another.
class Box {
double width;
double height;
double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w; height = h; depth = d;
} 17
Construct copy of an Object
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}

18
Construct copy of an Object
class OverloadCons2 {
public static void main(String args[]) {
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
Box myclone = new Box(mybox1);
double vol;
vol = mybox1.volume(); // get volume of first box
System.out.println("Volume of mybox1 is " + vol);
vol = mybox2.volume(); // get volume of second box
System.out.println("Volume of mybox2 is " + vol);
vol = mycube.volume(); // get volume of cube
System.out.println("Volume of cube is " + vol);
vol = myclone.volume(); // get volume of clone
System.out.println("Volume of clone is " + vol);
}
}

Volume of mybox1 is 3000.0


Volume of mybox2 is -1.0
Volume of cube is 343.0
Volume of clone is 3000.0
19
Argument Passing
 There are two ways that a computer language can pass an
argument to a method.
 Call-by-value
– Copies the value of an argument into the formal parameter of the method.
– Changes made to the parameter of the method have no effect on the
argument.
 Call-by-reference
– A reference to an argument (not the value of the argument) is passed to
the parameter.
– Inside the method, this reference is used to access the actual
argument specified in the call.
– Changes made to the parameter will affect the argument used to call the
method.

20
Argument Passing
 In Java, when we pass a primitive type to a method, it is
passed by value.
 When we pass an object to a method, it is implicitly passed by
reference.
– When we create a variable of a class type, we are only creating a
reference to an object.
– When we pass this reference to a method, the parameter that receives
it will refer to the same object as that referred to by the argument.
– Changes made to the object inside the method do affect the object
used as an argument.

21
Argument Passing
// 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;
}
}

22
Argument Passing
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);
}
}
ob.a and ob.b before call: 15 20
ob.a and ob.b after call: 30 23
10
Returning Objects
 Since all objects are dynamically allocated using new,
we don’t need to worry about an object going out-of-
scope when the method in which it was created
terminates.
 The object will continue to exist as long as there
is a reference to it somewhere in our program.
 When there are no references to it, the object will be
reclaimed the next time garbage collection takes place.

24
Returning Objects
// Returning an object.
class Test {
int a;
Test(int i) {
a = i;
}

Test incrByTen() {
Test temp = new Test(a+10);
return temp;
}
}

25
Returning Objects
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);
}
}
ob1.a: 2
ob2.a: 12
26
ob2.a after second increase: 22
Access Control
 How a member can be accessed is determined by the

access specifier that modifies its declaration.

 Java’s access specifiers are public, private, protected,

and a default access level.

 When no access specifier is used, then by default the

member of a class is public within its own package, but

cannot be accessed outside of its package.


27
Access Control
/* Demonstrates the difference between public, private, and default.*/
class Test {
int a; // default access
public int b; // public access
private int c; // private access
// methods to access c
void setc(int i) { // set c's value
c = i;
}
int getc() { // get c's value
return c;
}
}

28
Access Control
class AccessTest {
public static void main(String args[]) {
Test ob = new Test();
// These are OK, a and b may be accessed directly
ob.a = 10;
ob.b = 20;
// This is not OK and will cause an error
// ob.c = 100; // Error!
// You must access c through its methods
ob.setc(100); // OK
System.out.println("a, b, and c: " + ob.a + " " + ob.b + " " + ob.getc());
}
}

29
static
 We may want to define a class member that will be used
independently of any object of that class.
 To create such a member, precede its declaration with the
keyword static.
 Instance variables declared as static are, essentially, global
variables.
– When objects of its class are declared, no copy of a static
variable is made.
– Instead, all instances of the class share the same static
variable.

30
static
 Methods declared as static have several restrictions:
– They can only call other static methods.
– They must only access static data.
– They cannot refer to this or super in any way.
 If we need to do computation in order to initialize our
static variables, we can declare a static block which
gets executed exactly once, when the class is first
loaded.

31
static
// Demonstrate static variables, methods, and blocks.
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42); // static method is called
}
} Static block initialized.
x = 42
a=3
b = 12 32
final
 A variable can be declared as final. Doing so prevents
its contents from being modified.
 This means that we must initialize a final variable when
it is declared.
 For example:
– final int FILE_NEW = 1;
– final int FILE_OPEN = 2;
– final int FILE_SAVE = 3;
– final int FILE_SAVEAS = 4;
– final int FILE_QUIT = 5;
 Variables declared as final do not occupy memory on
a per-instance basis.
33
Recommended Reading
 Pages 125-143, Chapter # 7: A Closer Look at
Methods and Classes from Herbert Schildt,
Java: The Complete Reference, J2SETM 5 Edition

34

You might also like