PSOOP-Lecture 03-24-25
PSOOP-Lecture 03-24-25
Lecture-03
Outline
• Constructor Overloading Concept
• Constructor Overloading Examples
• Method Overloading Concept
• Method Overloading Examples
• Arrays in Java
Constructor Overloading
• We can define multiple constructors within the same class, each with
a distinct number/type of parameters.
• This concept is known as Constructor Overloading
• To understand this concept, consider the following example: Suppose
you want to buy a pen from a shop. In OOP, this pen is an object. Let
us design the Pen class.
• The Pen class can have data members inkColor, brand and price.
Constructor Overloading
class Pen{
String inkColor,brand;
float price;
}
Scenario 1:
You go to the shopkeeper and tell him you want
to buy a pen. The shopkeeper would give you
the most common pen that people usually buy.
This is a default (no-argument) constructor.
Let’s add it to the Pen class.
Constructor Overloading
class Pen{
String inkColor,brand;
float price;
Pen(){//give me the most common pen
inkColor=“blue”;
Scenario 2:
brand=“Cello”; You go to the shopkeeper and tell him you want
price=10; to buy a pen with red ink. The shopkeeper
} would give you the pen with red ink which
most people usually buy. This is a
}
parameterized constructor with one parameter.
Let’s add it to the Pen class.
Constructor Overloading
class Pen{
String inkColor, brand; Scenario 3:
float price; You go to the shopkeeper and tell him you want to
Pen(){//give me the most common pen buy a pen with green ink, brand Montex and 20
Rs(better quality). The shopkeeper would give you
inkColor=“blue”;
the pen with required features. This is another
brand=“Cello”; parameterized constructor with three parameters.
price=10; Let’s add it to the Pen class.
}
Pen(String inkColor){//give me a red pen (or
specific color pen)
this.inkColor=inkColor;
brand=“Cello”;
price=10;
}
}
Constructor Overloading
class Pen{
String inkColor, brand; Scenario 4:
float price; Now let’s say you go to the shopkeeper with a
Pen(){//give me the most common pen specific pen and say to the Shopkeeper you want
inkColor=“blue”;
brand=“Cello”; exactly that pen. This is another parameterized
price=10; constructor with object as parameter. The object is
} the same as the class. This is called Copy
Pen(String inkColor){ Constructor. You want an exact copy(replica) of the
//give me a red pen (or specific color pen)
this.inkColor=inkColor; Pen you are having with you. Let’s add it to the Pen
brand=“Cello”; class.
price=10;
}
Pen(String inkColor, String brand, float price){
//give me a pen with given features
this.inkColor=inkColor;
this.brand=brand;
this.price=price;
}
}
class Pen{
String inkColor, brand;
float price;
Pen(){//give me the most common pen
inkColor=“blue”; The class we designed is a class which contains
brand=“Cello”; overloaded constructors and also a display()
price=10; method. Lets create objects of this class and test it
}
Pen(String inkColor){ from main()
//give me a red pen (or specific color pen)
this.inkColor=inkColor;
brand=“Cello”;
price=10;
}
Pen(String inkColor, String brand, float price){
//give me a pen with given features
this.inkColor=inkColor;
this.brand=brand;
this.price=price;
}
Pen(Pen p){//give me an exact replica of this pen
inkColor=p.inkColor;
brand=p.brand;
price=p.price;
}
void display(){
System.out.println(“Color=“+inkColor+”\nBrand=“+brand+”\nPrice=“+price);
}
}
Class TestPen
class TestPen{
public static void main(String []args){
Pen p1=new Pen();//no argument constructor call
Pen p2=new Pen("red");//parameterized constructor 1 called
Pen p3=new Pen("green","Montex",20);//parameterized constructor 2 called
Pen p4=new Pen(p3);//copy constructor
p1.display();
p2.display();
p3.display();
p4.display();
}
}
Method Overloading Concept
int [][] x;
//x is a reference to an array of int arrays
x = new int[3][4];
//Create 3 new int arrays, each having 4 elements
//x[0] refers to the first int array, x[1] to the second and
so on
//x[0][0] is the first element of the first array
//x.length will be 3
//x[0].length, x[1].length and x[2].length will be 4
THANK YOU