Oop Using Java - Unit II
Oop Using Java - Unit II
Output:
Overloading Methods - Continued
● Java looks for a match between the arguments used to call an overloaded
method. however, match need not always be exact.
● In some cases, Java automatically type convert the overload resolution.
An example
// Automatic type conversions apply to overloading. class Overload2 {
class OverloadDemo { public static void main(String[] args) {
void test() { OverloadDemo ob = new OverloadDemo();
System.out.println("No parameters"); int i = 88;
}
Output:
Using Objects as Parameters
● We can pass objects to methods as params
class Test { class PassOb {
int a, b; public static void main(String args[]) {
Test ob1 = new Test(100, 22);
Test(int i, int j) { Test ob2 = new Test(100, 22);
a = i; Test ob3 = new Test(-1, -1);
b = j;
} System.out.println("ob1 == ob2: " +
ob1.equalTo(ob2));
// return true if o is equal to the invoking object System.out.println("ob1 == ob3: " +
boolean equalTo(Test o) { ob1.equalTo(ob3));
if (o.a == a && o.b == b) }
return true; }
else
return false;
}
}
Using Objects as Parameters
● the equalTo() method inside test compares two objects for equality and then
returns the result.
● it compares the invoking object with the one that is passed.
● if they contain the same values, then the method returns true. otherwise it
returns false.
● the parameter o in equalTo() specifies Test as its type.
Using Objects as Parameters
● We can 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.
● Example: public class OverloadCons2 {
public static void main (String args []) {
// create boxes using the various constructors
Box mybox1 = new Box (10, 20, 15);
class Box { // constructor used when no Box mybox2 = new Box ();
Box mycube = new Box (7);
double width; dimensions specified
double height; Box() { Box myclone = new Box (mybox1 ); // create copy of mybox1
double depth;
width = -1; // use -1 to indicate double vol ;
// constructor used when all dimensions System .out .println ("Volume of myclone is " + vol );
}
specified }
// compute and return volume
Box(double w, double h, double d) {
double volume() {
width = w;
return width * height * depth;
height = h;
depth = d; }
} }
A closer look at Argument Passing
- there are two ways a computer language can pass arguments to a function
- call by value : the value of the argument is copied into the function parameter
- call by reference : the argument is a reference to an object in memory
- although java uses call by value, to pass all arguments, the precise effect differs
between whether a primitive type or a reference type is passed
- when a primitive type is passed, the value of the argument is copied into the function
parameter public class CallByValue {
public static void main(String args[]) {
class Test { Test ob = new Test();
Test(int i, int j) {
a = i; System.out.println("ob.a and ob.b before
}
ob.meth(ob);
// pass an object
void meth(Test o) { System.out.println("ob.a and ob.b after
call: " + ob.a + " " + ob.b);
o.a *= 2;
o.b /= 2; }
} }
}
Returning Objects
● a method can return any type of data, including class types that you create.
● Example:
public class RetOb {
public static void main(String args[]) {
Test ob1 = new Test(2);
Test ob2;
// Returning an object
class Test {
ob2 = ob1.incrByTen();
int a;
Test incrByTen() {
System.out.println("ob2.a after second
Test temp = new Test(a + 10);
increase: " + ob2.a);
return temp;
}
}
}
}
Recursion
● Java supports Recursion
● Recursion is a process of defining something in terms of itself.
● Recursion is the attribute that allows a method to call itself.
● A method that calls itself is said to be recursive.
RecTest(int i) { int i;
values = new int[i];
} for (i = 0; i < 10; i++)
ob.values[i] = i;
// display array -- recursively
void printArray(int i) { ob.printArray(10);
if (i == 0) }
return; }
else
printArray(i - 1);
System.out.println("[" + (i - 1) + "] " +
values[i - 1]);
}
}
Introducing Access Control
● encapsulation links data with the code that manipulates it.
● encapsulation provides another important attribute: access control
● through encapsulation, you can control what parts of a program can access the
members of a class.
● by controlling access, you can prevent misuse.
● for example: allowing access to data only through a well-defined set of methods,
you can prevent the misuse of that data.
● thus, when correctly implemented, a class creates a “black box” which may be used,
but the inner workings of which are not open to tampering.
● how a member can be accessed is determined by the access modifier attached to
its declaration.
● some aspects of access control are related mostly to inheritance or packages.
● A package is essentially a collection of classes.
Java’s access modifiers
● public, private, protected
● java also defines default access level.
● protected applies only when inheritance is involved.
● when a member of a class is modified by public, then that member can be
accessed by any other code.
● when a member of a class is specified as private, then that member can only
be accessed by other members of its class.
Introducing Access Control
public class AccessTest {
public static void main(String args[]) {
Test ob = new Test();
/* This program demonstrates the
difference between public and private. */
// These are OK, a and b may be accessed
class Test {
directly
int a; // default access
ob.a = 10;
public int b; // public access
ob.b = 20;
private int c; // private access
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
Introducing Nested and Inner Classes
● it is possible to define a class within another class; such classes are known as
nested classes.
● the scope of a nested class is bounded by the scope of its enclosing class
● thus, if class B is defined within class A, then B does not exist independently
of A.
● A nested class has access to the members, including private members, of the
class in which it is nested. however, the enclosing class does not have access
to the members of the nested class.
● a nested class that is directly declared within its enclosing class scope is a
member of its enclosing class.
Nested Classes - Types
● there are two types of nested classes
○ static
○ non-static
● a static nested class is one that has the static modifier applied.
● because it is static, it must access the non-static members of its enclosing class
through an object.
● that is, it cannot refer to non-static members of its enclosing class directly.
● because of this restriction, static nested classes are seldom used.
● the most important type of nested class is the inner class.
● an inner class is a non-static nested class.
● it has access to all of the variables and methods of its outer class and may refer to
them directly in the same way that other non-static members of the outer class do.
Nested Classes - Example
inner.display(); }
System.out.println(strOb1);
System.out.println(strOb2);
System.out.println(strOb3);
}
}
String class
public class StringDemo2 {
public static void main(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1;
if (strOb1.equals(strOb2)) {
System.out.println("strOb1 == strOb2");
} else {
System.out.println("strOb1 != strOb2");
}
if (strOb1.equals(strOb3)) {
System.out.println("strOb1 == strOb3");
} else {
System.out.println("strOb1 != strOb3");
}
}
}
String Class
for (int x : v) {
System.out.print(x + " ");
}
System.out.println();
}
vaTest(n1);
vaTest(n2);
vaTest(n3);
}
}
Demonstrate - Variable length arguments
System.out.println();
}
System.out.println();
}
System.out.println("vaTest(int...): " + }
Contents: "); }
}
System.out.println();
}
Varargs and Ambiguity
for (int x : v) {
System.out.print(x + " "); System.out.println();
} }
System.out.println();
}
End of Unit II: A Closer look at Methods and Classes