Académique Documents
Professionnel Documents
Culture Documents
(Chapter 7)
Overloading
Polymorphism
Compile Time or Static Polymorphism
Overloading
Overloading Methods
class Calculator {
public static int add(int a, int b) {
return a + b;
}
public static int add(int a, int b, int c) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
Overloading
public static double add(int a, double b) {
return a + b;
}
public static double add(double a, int b) {
return a + b;
}
}
Overloading
class UseCalculator {
public static void main(String[] args) {
System.out.println(add(2, 3));
System.out.println(add(1, 2, 3));
System.out.println(add(34.5, 24.6));
System.out.println(add(5, 38.8));
System.out.println(add(56.4, 25));
}
}
Overloading Constructors
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;
} //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; } }
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; //get volume of first box
vol = 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);
}
}
Argument Passing
call-by-value
Method copies the value of an argument into the formal parameter of the
subroutine.
Therefore, changes made to the parameter of the subroutine have no effect
on the argument
call-by-reference
// call by value
class ValueTest {
public static void main (String [] args) {
int a = 1;
ValueTest vt = new ValueTest();
System.out.println("Before modify() a = " + a);
vt.modify(a);
System.out.println("After modify() a = " + a);
}
void modify(int number) {
number = number + 1;
System.out.println("number = " + number);
}
}
import java.awt.Dimension;
class ReferenceTest {
public static void main (String [] args) {
Dimension d = new Dimension(5,10);
ReferenceTest rt = new ReferenceTest();
System.out.println("Before modify() d.height = " + d.height);
rt.modify(d);
System.out.println("After modify() d.height = "+ d.height);
}
void modify(Dimension dim) {
dim.height = dim.height + 1;
System.out.println("dim = " + dim.height);
}
}
static
Final
Types of Variables
IV:
LV:
FV:
SV:
Primitive variables
IV are not allowed inside the static block and static method directly
(Can declare, but could not access).
Instance method calling is not allowed inside static block and static
method directly.
IV, SV, Instance methods and static methods are allowed inside the
instance method directly.
We can call the instance variables and instance method with object
because instance method belongs to object.
Static variables and static methods can be called with class name
because static members belongs to class.
Inner Classes
class Outer {
void test() {
inner.display();
// System.out.println(a);
class Inner {
int a = 2;
void display() {
}}
// System.out.println(a);
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}
}
Command-Line Arguments
Arrays
Initialize
Input data
Output stored data
Find largest/smallest/sum/average of
elements
Example 9_3
double[] sales = new double[10];
int index;
double largestSale, sum, average;
Integer.parseInt()
Tedious job to enter the values each time before the compilation in the
method itself. Now if we want to enter an integer value after the compilation
of a program and force the JVM to ask for an input, then we should use
Integer.parseInt(string str).
if (a > 0)
System.out.println("The number is positive");
else
System.out.println("The number is negative");
}
}
class He{
int l,w;
void show(int b,int a){
l=b; w=a;
}
int cal(){
return l*w;
}
}
class ParseInt{
public static void main(String arg[]){
He h=new He();
int d = Integer.parseInt(arg[0]);
int e = Integer.parseInt(arg[1]);
h.show(d,e);
System.out.println(" you have entered these values : " + d + " and " + e);
int area = h.cal();
System.out.println(" area of a rectange is : " + area);
}
}