Compiled By: Dr.Engr.Muhammad Imran Saleem
1
Method & Constructor
Overloading
Course Books
 Text Book:
 Herbert Schildt, Java: The Complete Reference, McGraw-Hill
Education, Eleventh Edition
 Craig Larman, Applying UML & patterns, 2 edition
 Reference Books:
 Cay S. Horstmann, Big Java: Early Objects, Wiley, 7th Edition
 Herbert Schildt, Java: A Beginner's Guide, McGraw-Hill Education,
Eighth Edition
2
Topics Covered
 Method Overloading with example
 Why Method Overloading?
 Constructor Overloading with example
 Method Overriding with example
3
Method Overloading
 A class may define multiple methods with the same
name---this is called method overloading
 usually perform the same task on different data types
 Example: The PrintStream class defines multiple println
methods, i.e., println is overloaded:
println (String s)
println (int i)
println (double d)
…
 The following lines use the System.out.print method for
different data types:
System.out.println ("The total is:");
double total = 0;
System.out.println (total);
4
Method Overloading
5
Different ways to Overload a method
Three ways to overload a method
In order to overload a method, the argument lists of the
methods must differ in either of these:
1. Number of parameters.
For example: This is a valid case of overloading
add(int, int)
add(int, int, int)
2. Data type of parameters.
For example:
add(int, int)
add(int, float)
6
Different ways to Overload a method
3. Sequence of Data type of parameters.
For example:
add(int, float)
add(float, int)
 In java, Method Overloading is not possible by changing
the return type of the method
 The compiler treats overloaded methods as completely
different methods.
 The compiler knows which one to call based on the
number and the types of the arguments
7
8
Method Overloading: Signature
 The compiler must be able to determine which
version of the method is being invoked
 This is by analyzing the parameters, which form
the signature of a method
 the signature includes the type and order of the
parameters
 if multiple methods match a method call, the compiler picks
the best match
 if none matches exactly but some implicit conversion can be
done to match a method, then the method is invoke with
implicit conversion.
 the return type of the method is not part of the
signature
Method Overloading
double tryMe (int x)
{
return x + .375;
}
Version 1
double tryMe (int x, double y)
{
return x * y;
}
Version 2
result = tryMe (25, 4.32)
Invocation
More Examples
double tryMe ( int x )
{
return x + 5;
}
double tryMe ( double x )
{
return x * .375;
}
double tryMe (double x, int y)
{
return x + y;
}
tryMe( 1 );
tryMe( 1.0 );
tryMe( 1.0, 2);
tryMe( 1, 2);
tryMe( 1.0, 2.0);
Which tryMe will be called?
Method Overloading
 Following are a few pointers that we have to
keep in mind while overloading methods in
Java.
 We cannot overload a return type.
 Although we can overload static methods, the
arguments or input parameters have to be
different.
11
Method Overloading
 We cannot overload two methods if they only
differ by a static keyword.
 Like other static methods, the main() method
can also be overloaded.
12
Method Overloading Example
13
# Example 01
class MotorBike{
private String startMethod = "Kick";
public void start(){
System.out.println(startMethod + " starting ....");
}
public void start(String method){
this.startMethod = method;
System.out.println(startMethod + " starting ....");
}
}
Method Overloading Example
14
# Example 01 cont..
public class DemoApp{
public static void main(String[] arg){
MotorBike motorBike = new MotorBike();
motorBike.start();
motorBike.start("Self");
}
}
/*********************** OUT PUT ****************************/
Kick starting ....
Self starting ....Process finished with exit code 0
Method Overloading Example
15
# Example 02
class Sum
{
int add(int n1, int n2)
{
return n1+n2;
}
int add(int n1, int n2, int n3)
{
return n1+n2+n3;
}
int add(int n1, int n2, int n3, int n4)
{
return n1+n2+n3+n4;
}
int add(int n1, int n2, int n3, int n4, int n5)
{
return n1+n2+n3+n4+n5;
}
Method Overloading Example
16
# Example 02 cont..
public static void main(String args[])
{
Sum obj = new Sum();
System.out.println("Sum of two numbers: "+obj.add(20, 21));
System.out.println("Sum of three numbers: "+obj.add(20, 21, 22));
System.out.println("Sum of four numbers: "+obj.add(20, 21, 22, 23));
System.out.println("Sum of five numbers: "+obj.add(20, 21, 22, 23, 24));
}
}
Method Overloading Example
17
# Example 02 cont..
Output
Sum of two numbers: 41
Sum of three numbers: 63
Sum of four numbers: 86
Sum of five numbers: 110
Method Overloading Example
18
# Example 02 cont..
Output
Sum of two numbers: 41
Sum of three numbers: 63
Sum of four numbers: 86
Sum of five numbers: 110
Method Overloading Example
19
# Example 03: Program to overload static methods in java.
public class Test{
public static int func(int a ){
return 100;
}
public static char func(int a , int b){
return “abc";
}
public static void main(String args[]){
System.out.println(func(1));
System.out.println(func(1,3));
}
}
Output:
100
abc
Method Overloading and Type Promotion
Method Overloading and Type Promotion
1. class OverloadingCalculation1{
2. void sum(int a,long b)
3. { System.out.println(a+b); }
4. void sum(int a,int b,int c)
5. { System.out.println(a+b+c); }
6.
7. public static void main(String args[]){
8. OverloadingCalculation1 obj=new OverloadingCalculation1();
9. obj.sum(20,20);
//now second int literal will be promoted to long
10. obj.sum(20,20,20);
11.
12. }
13. }
Note: One type is not de-
promoted implicitly for
example double cannot
be de-promoted to any
type implicitly.
Lets see few Valid/invalid cases of method
overloading
 Case 1:
int mymethod(int a, int b, float c)
int mymethod(int var1, int var2, float var3)
Result: Compile time error. Argument lists are exactly same. Both
methods are having same number, data types and same sequence of
data types.
 Case 2:
int mymethod(int a, int b)
int mymethod(float var1, float var2)
Result: Perfectly fine. Valid case of overloading. Here data types of
arguments are different.
22
 Case 3:
int mymethod(int a, int b)
int mymethod(int num)
Result: Perfectly fine. Valid case of overloading. Here number of
arguments are different.
 Case 4:
float mymethod(int a, float b)
float mymethod(float var1, int var2)
Result: Perfectly fine. Valid case of overloading. Sequence of the data
types of parameters are different, first method is having (int, float) and
second is having (float, int).
23
Lets see few Valid/invalid cases of method
overloading
 Case 5:
int mymethod(int a, int b)
float mymethod(int var1, int var2)
Result: Compile time error. Argument lists are exactly same. Even
though return type of methods are different, it is not a valid case. Since
return type of method doesn’t matter while overloading a method.
24
Lets see few Valid/invalid cases of method
overloading
Why Method Overloading ??
 The main advantage of using method
overloading in Java is that it gives us the liberty
to not define a function again and again for
doing the same thing.
25
Constructor Overloading
 In addition to overloading methods, we can also
overload constructors in java.
 Constructor overloading is a concept of having
more than one constructor with different
parameters list, in such a way so that each
constructor performs a different task.
26
Constructor Overloading
 If a class has more than one constructor, they
are “overloaded” and must have different
numbers and/or types of arguments.
 Programmers often provide a “no-args”
constructor that takes no arguments.
 If a programmer does not define any
constructors, Java provides one default no-args
constructor, which allocates memory and sets
fields to the default values
 Overloaded constructor is called based upon the
parameters specified when new is executed.
27
Constructor Overloading
28
Constructor Overloading Example
29
# Example 01:
class StudentData
{
int stuID;
String stuName;
int stuAge;
StudentData()
{
//Default constructor
stuID = 100;
stuName = "New Student";
stuAge = 18;
}
StudentData(int num1, String str, int num2)
{
//Parameterized constructor
stuID = num1;
stuName = str;
stuAge = num2;
}
Constructor Overloading Example
30
# Example 01 cont…
public static void main(String args[])
{
//This object creation would call the default constructor
StudentData myobj = new StudentData();
System.out.println("Student Name is: "+myobj.StuName);
System.out.println("Student Age is: "+myobj.StuAge);
System.out.println("Student ID is: "+myobj.StuID);
/*This object creation would call the parameterized
* constructor StudentData(int, String, int)*/
StudentData myobj2 = new StudentData(555, "Chaitanya", 25);
System.out.println("Student Name is: "+myobj2.StuName);
System.out.println("Student Age is: "+myobj2.StuAge);
System.out.println("Student ID is: "+myobj2.StuID);
}
}
Constructor Overloading Example
31
# Example 01 cont…
Output:
Student Name is: New Student
Student Age is: 18
Student ID is: 100
Student Name is: Chaitanya
Student Age is: 25
Student ID is: 555
32
This() in Constructor
 One constructor can “call” another constructor in the same
class, but there are special rules
 You call the other constructor with the keyword this
 The call must be the very first thing the constructor does
 Point(int x, int y) {
this.x = x;
this.y = y;
sum = x + y;
}
 Point() {
this(0, 0);
}
 A common reason for overloading constructors is (as
above) to provide default values for missing parameters
Role of this() in Constructor
Overloading
33
# Example 02
public class OverloadingExample2
{
private int rollNum;
OverloadingExample2()
{
rollNum =100;
}
OverloadingExample2(int rnum)
{
this();
/*this() is used for calling the default
* constructor from parameterized constructor.
* It should always be the first statement
* inside constructor body.
*/
rollNum = rollNum+ rnum;
}
Role of this() in Constructor
Overloading
34
# Example 02 cont….
public int getRollNum() {
return rollNum;
}
public void setRollNum(int rollNum) {
this.rollNum = rollNum;
}
public static void main(String args[])
{
OverloadingExample2 obj = new OverloadingExample2(12);
System.out.println(obj.getRollNum());
}
}
Role of this() in Constructor
Overloading
35
# Example 02 cont….
Output :
112
As you can see in the above program that we
called the parameterized constructor during
object creation. Since we have this() placed in
parameterized constructor, the default
constructor got invoked from it and initialized the
variable rollNum.
Role of this() in Constructor
Overloading
36
# Example 03: Guess the output of the following program
public class OverloadingExample2
{
private int rollNum;
OverloadingExample2()
{
rollNum =100;
}
OverloadingExample2(int rnum)
{
rollNum = rollNum+ rnum;
this();
}
Role of this() in Constructor
Overloading
37
# Example 03: Guess the output of the following program
public int getRollNum() {
return rollNum;
}
public void setRollNum(int rollNum) {
this.rollNum = rollNum;
}
public static void main(String args[])
{
OverloadingExample2 obj = new OverloadingExample2(12);
System.out.println(obj.getRollNum());
}
}
Role of this() in Constructor
Overloading
38
# Example 03 cont….
Output :
Exception in thread "main" java.lang.Error: Unresolved compilation
problem:Constructor call must be the first statement in a constructor
Program gave a compilation error.
Reason: this() should be the first statement
inside a constructor..
Default Constructor
 If a programmer does not define any
constructors, Java provides one default no-args
constructor, which allocates memory and sets
fields to the default values
class Bird {
int i;}
public class DefaultConstructor {
public static void main(String args[]) {
Bird nc = new Bird(); // default!
}
}
Constructor Overloading
40
# Example 04
public class Demo
{
int rollNum;
//We are not defining a no-arg constructor here
Demo(int rnum)
{
rollNum = rollNum+ rnum;
}
public static void main(String args[])
{
//This statement would invoke no-arg constructor
Demo obj = new Demo();
}
}
Constructor Overloading
41
# Example 04
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation
problem:The constructor Demo() is undefined
COPY CONSTRUCTOR:
 If
Sphere a=b;
b is also of sphere type a therefore a and b points to the
same obj reference. But you will not have distinct
objects.
 For distinct objects use Copy constructor.
Sphere(final Sphere OldSphere)
{
radius = OldSphere.radius; // Set the radius
// Set the coordinates of the center
xCenter = OldSphere. xCenter;
yCenter = OldSphere .yCenter;
zCenter = OldSphere. zCenter;
}
Constructor Overloading
43
 Another important point to note while
overloading a constructor is:
When we don’t implement any constructor,
the java compiler inserts the default
constructor into our code during compilation
 However, if we implement any constructor
then compiler doesn’t do it.
Why Constructor Overloading ??
 Sometimes there is a need of initializing an
object in different ways.
 This can be done using constructor
overloading.
 For e.g. Vector class has 4 types of
constructors.
44
Why Constructor Overloading ??
 If you do not want to specify the initial capacity
and capacity increment then you can simply
use default constructor of Vector class like this
Vector v = new Vector()
 However, if you need to specify the capacity
and increment then you call the parameterized
constructor of Vector class with two int
arguments like this:
 Vector v= new Vector(10, 5);
45

Lecture_7 Method Overloading.pptx

  • 1.
    Compiled By: Dr.Engr.MuhammadImran Saleem 1 Method & Constructor Overloading
  • 2.
    Course Books  TextBook:  Herbert Schildt, Java: The Complete Reference, McGraw-Hill Education, Eleventh Edition  Craig Larman, Applying UML & patterns, 2 edition  Reference Books:  Cay S. Horstmann, Big Java: Early Objects, Wiley, 7th Edition  Herbert Schildt, Java: A Beginner's Guide, McGraw-Hill Education, Eighth Edition 2
  • 3.
    Topics Covered  MethodOverloading with example  Why Method Overloading?  Constructor Overloading with example  Method Overriding with example 3
  • 4.
    Method Overloading  Aclass may define multiple methods with the same name---this is called method overloading  usually perform the same task on different data types  Example: The PrintStream class defines multiple println methods, i.e., println is overloaded: println (String s) println (int i) println (double d) …  The following lines use the System.out.print method for different data types: System.out.println ("The total is:"); double total = 0; System.out.println (total); 4
  • 5.
  • 6.
    Different ways toOverload a method Three ways to overload a method In order to overload a method, the argument lists of the methods must differ in either of these: 1. Number of parameters. For example: This is a valid case of overloading add(int, int) add(int, int, int) 2. Data type of parameters. For example: add(int, int) add(int, float) 6
  • 7.
    Different ways toOverload a method 3. Sequence of Data type of parameters. For example: add(int, float) add(float, int)  In java, Method Overloading is not possible by changing the return type of the method  The compiler treats overloaded methods as completely different methods.  The compiler knows which one to call based on the number and the types of the arguments 7
  • 8.
    8 Method Overloading: Signature The compiler must be able to determine which version of the method is being invoked  This is by analyzing the parameters, which form the signature of a method  the signature includes the type and order of the parameters  if multiple methods match a method call, the compiler picks the best match  if none matches exactly but some implicit conversion can be done to match a method, then the method is invoke with implicit conversion.  the return type of the method is not part of the signature
  • 9.
    Method Overloading double tryMe(int x) { return x + .375; } Version 1 double tryMe (int x, double y) { return x * y; } Version 2 result = tryMe (25, 4.32) Invocation
  • 10.
    More Examples double tryMe( int x ) { return x + 5; } double tryMe ( double x ) { return x * .375; } double tryMe (double x, int y) { return x + y; } tryMe( 1 ); tryMe( 1.0 ); tryMe( 1.0, 2); tryMe( 1, 2); tryMe( 1.0, 2.0); Which tryMe will be called?
  • 11.
    Method Overloading  Followingare a few pointers that we have to keep in mind while overloading methods in Java.  We cannot overload a return type.  Although we can overload static methods, the arguments or input parameters have to be different. 11
  • 12.
    Method Overloading  Wecannot overload two methods if they only differ by a static keyword.  Like other static methods, the main() method can also be overloaded. 12
  • 13.
    Method Overloading Example 13 #Example 01 class MotorBike{ private String startMethod = "Kick"; public void start(){ System.out.println(startMethod + " starting ...."); } public void start(String method){ this.startMethod = method; System.out.println(startMethod + " starting ...."); } }
  • 14.
    Method Overloading Example 14 #Example 01 cont.. public class DemoApp{ public static void main(String[] arg){ MotorBike motorBike = new MotorBike(); motorBike.start(); motorBike.start("Self"); } } /*********************** OUT PUT ****************************/ Kick starting .... Self starting ....Process finished with exit code 0
  • 15.
    Method Overloading Example 15 #Example 02 class Sum { int add(int n1, int n2) { return n1+n2; } int add(int n1, int n2, int n3) { return n1+n2+n3; } int add(int n1, int n2, int n3, int n4) { return n1+n2+n3+n4; } int add(int n1, int n2, int n3, int n4, int n5) { return n1+n2+n3+n4+n5; }
  • 16.
    Method Overloading Example 16 #Example 02 cont.. public static void main(String args[]) { Sum obj = new Sum(); System.out.println("Sum of two numbers: "+obj.add(20, 21)); System.out.println("Sum of three numbers: "+obj.add(20, 21, 22)); System.out.println("Sum of four numbers: "+obj.add(20, 21, 22, 23)); System.out.println("Sum of five numbers: "+obj.add(20, 21, 22, 23, 24)); } }
  • 17.
    Method Overloading Example 17 #Example 02 cont.. Output Sum of two numbers: 41 Sum of three numbers: 63 Sum of four numbers: 86 Sum of five numbers: 110
  • 18.
    Method Overloading Example 18 #Example 02 cont.. Output Sum of two numbers: 41 Sum of three numbers: 63 Sum of four numbers: 86 Sum of five numbers: 110
  • 19.
    Method Overloading Example 19 #Example 03: Program to overload static methods in java. public class Test{ public static int func(int a ){ return 100; } public static char func(int a , int b){ return “abc"; } public static void main(String args[]){ System.out.println(func(1)); System.out.println(func(1,3)); } } Output: 100 abc
  • 20.
    Method Overloading andType Promotion
  • 21.
    Method Overloading andType Promotion 1. class OverloadingCalculation1{ 2. void sum(int a,long b) 3. { System.out.println(a+b); } 4. void sum(int a,int b,int c) 5. { System.out.println(a+b+c); } 6. 7. public static void main(String args[]){ 8. OverloadingCalculation1 obj=new OverloadingCalculation1(); 9. obj.sum(20,20); //now second int literal will be promoted to long 10. obj.sum(20,20,20); 11. 12. } 13. } Note: One type is not de- promoted implicitly for example double cannot be de-promoted to any type implicitly.
  • 22.
    Lets see fewValid/invalid cases of method overloading  Case 1: int mymethod(int a, int b, float c) int mymethod(int var1, int var2, float var3) Result: Compile time error. Argument lists are exactly same. Both methods are having same number, data types and same sequence of data types.  Case 2: int mymethod(int a, int b) int mymethod(float var1, float var2) Result: Perfectly fine. Valid case of overloading. Here data types of arguments are different. 22
  • 23.
     Case 3: intmymethod(int a, int b) int mymethod(int num) Result: Perfectly fine. Valid case of overloading. Here number of arguments are different.  Case 4: float mymethod(int a, float b) float mymethod(float var1, int var2) Result: Perfectly fine. Valid case of overloading. Sequence of the data types of parameters are different, first method is having (int, float) and second is having (float, int). 23 Lets see few Valid/invalid cases of method overloading
  • 24.
     Case 5: intmymethod(int a, int b) float mymethod(int var1, int var2) Result: Compile time error. Argument lists are exactly same. Even though return type of methods are different, it is not a valid case. Since return type of method doesn’t matter while overloading a method. 24 Lets see few Valid/invalid cases of method overloading
  • 25.
    Why Method Overloading??  The main advantage of using method overloading in Java is that it gives us the liberty to not define a function again and again for doing the same thing. 25
  • 26.
    Constructor Overloading  Inaddition to overloading methods, we can also overload constructors in java.  Constructor overloading is a concept of having more than one constructor with different parameters list, in such a way so that each constructor performs a different task. 26
  • 27.
    Constructor Overloading  Ifa class has more than one constructor, they are “overloaded” and must have different numbers and/or types of arguments.  Programmers often provide a “no-args” constructor that takes no arguments.  If a programmer does not define any constructors, Java provides one default no-args constructor, which allocates memory and sets fields to the default values  Overloaded constructor is called based upon the parameters specified when new is executed. 27
  • 28.
  • 29.
    Constructor Overloading Example 29 #Example 01: class StudentData { int stuID; String stuName; int stuAge; StudentData() { //Default constructor stuID = 100; stuName = "New Student"; stuAge = 18; } StudentData(int num1, String str, int num2) { //Parameterized constructor stuID = num1; stuName = str; stuAge = num2; }
  • 30.
    Constructor Overloading Example 30 #Example 01 cont… public static void main(String args[]) { //This object creation would call the default constructor StudentData myobj = new StudentData(); System.out.println("Student Name is: "+myobj.StuName); System.out.println("Student Age is: "+myobj.StuAge); System.out.println("Student ID is: "+myobj.StuID); /*This object creation would call the parameterized * constructor StudentData(int, String, int)*/ StudentData myobj2 = new StudentData(555, "Chaitanya", 25); System.out.println("Student Name is: "+myobj2.StuName); System.out.println("Student Age is: "+myobj2.StuAge); System.out.println("Student ID is: "+myobj2.StuID); } }
  • 31.
    Constructor Overloading Example 31 #Example 01 cont… Output: Student Name is: New Student Student Age is: 18 Student ID is: 100 Student Name is: Chaitanya Student Age is: 25 Student ID is: 555
  • 32.
    32 This() in Constructor One constructor can “call” another constructor in the same class, but there are special rules  You call the other constructor with the keyword this  The call must be the very first thing the constructor does  Point(int x, int y) { this.x = x; this.y = y; sum = x + y; }  Point() { this(0, 0); }  A common reason for overloading constructors is (as above) to provide default values for missing parameters
  • 33.
    Role of this()in Constructor Overloading 33 # Example 02 public class OverloadingExample2 { private int rollNum; OverloadingExample2() { rollNum =100; } OverloadingExample2(int rnum) { this(); /*this() is used for calling the default * constructor from parameterized constructor. * It should always be the first statement * inside constructor body. */ rollNum = rollNum+ rnum; }
  • 34.
    Role of this()in Constructor Overloading 34 # Example 02 cont…. public int getRollNum() { return rollNum; } public void setRollNum(int rollNum) { this.rollNum = rollNum; } public static void main(String args[]) { OverloadingExample2 obj = new OverloadingExample2(12); System.out.println(obj.getRollNum()); } }
  • 35.
    Role of this()in Constructor Overloading 35 # Example 02 cont…. Output : 112 As you can see in the above program that we called the parameterized constructor during object creation. Since we have this() placed in parameterized constructor, the default constructor got invoked from it and initialized the variable rollNum.
  • 36.
    Role of this()in Constructor Overloading 36 # Example 03: Guess the output of the following program public class OverloadingExample2 { private int rollNum; OverloadingExample2() { rollNum =100; } OverloadingExample2(int rnum) { rollNum = rollNum+ rnum; this(); }
  • 37.
    Role of this()in Constructor Overloading 37 # Example 03: Guess the output of the following program public int getRollNum() { return rollNum; } public void setRollNum(int rollNum) { this.rollNum = rollNum; } public static void main(String args[]) { OverloadingExample2 obj = new OverloadingExample2(12); System.out.println(obj.getRollNum()); } }
  • 38.
    Role of this()in Constructor Overloading 38 # Example 03 cont…. Output : Exception in thread "main" java.lang.Error: Unresolved compilation problem:Constructor call must be the first statement in a constructor Program gave a compilation error. Reason: this() should be the first statement inside a constructor..
  • 39.
    Default Constructor  Ifa programmer does not define any constructors, Java provides one default no-args constructor, which allocates memory and sets fields to the default values class Bird { int i;} public class DefaultConstructor { public static void main(String args[]) { Bird nc = new Bird(); // default! } }
  • 40.
    Constructor Overloading 40 # Example04 public class Demo { int rollNum; //We are not defining a no-arg constructor here Demo(int rnum) { rollNum = rollNum+ rnum; } public static void main(String args[]) { //This statement would invoke no-arg constructor Demo obj = new Demo(); } }
  • 41.
    Constructor Overloading 41 # Example04 Output: Exception in thread "main" java.lang.Error: Unresolved compilation problem:The constructor Demo() is undefined
  • 42.
    COPY CONSTRUCTOR:  If Spherea=b; b is also of sphere type a therefore a and b points to the same obj reference. But you will not have distinct objects.  For distinct objects use Copy constructor. Sphere(final Sphere OldSphere) { radius = OldSphere.radius; // Set the radius // Set the coordinates of the center xCenter = OldSphere. xCenter; yCenter = OldSphere .yCenter; zCenter = OldSphere. zCenter; }
  • 43.
    Constructor Overloading 43  Anotherimportant point to note while overloading a constructor is: When we don’t implement any constructor, the java compiler inserts the default constructor into our code during compilation  However, if we implement any constructor then compiler doesn’t do it.
  • 44.
    Why Constructor Overloading??  Sometimes there is a need of initializing an object in different ways.  This can be done using constructor overloading.  For e.g. Vector class has 4 types of constructors. 44
  • 45.
    Why Constructor Overloading??  If you do not want to specify the initial capacity and capacity increment then you can simply use default constructor of Vector class like this Vector v = new Vector()  However, if you need to specify the capacity and increment then you call the parameterized constructor of Vector class with two int arguments like this:  Vector v= new Vector(10, 5); 45