0% found this document useful (0 votes)
2 views

PSOOP-Lecture 03-24-25

The document covers concepts of constructor and method overloading in Java, providing examples and scenarios for each. It explains how to define multiple constructors and methods with the same name but different signatures, as well as introduces arrays in Java, including their initialization and usage. Additionally, it discusses the characteristics of arrays, such as fixed size and data type homogeneity, along with examples of one-dimensional and multi-dimensional arrays.

Uploaded by

nikahatkazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PSOOP-Lecture 03-24-25

The document covers concepts of constructor and method overloading in Java, providing examples and scenarios for each. It explains how to define multiple constructors and methods with the same name but different signatures, as well as introduces arrays in Java, including their initialization and usage. Additionally, it discusses the characteristics of arrays, such as fixed size and data type homogeneity, along with examples of one-dimensional and multi-dimensional arrays.

Uploaded by

nikahatkazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

PSOOP

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

✓ Defining multiple methods with the same name but different


signatures within the same class

✓ Java compiler determines which method is used based on the


method signature

✓ Overloaded methods must have different parameter lists. You


cannot overload methods based on different modifiers or return
types
Method
Overloading
Example
Method Overloading Example contd..
What is the output?
Will this code compile?
What is the output of this code?
Arrays in Java
• A data structure which defines an ordered collection of a fixed number of
homogeneous data elements
• Size is fixed and cannot increase to accommodate more elements
• Arrays in Java are objects and can be of primitive data types or reference variable
type
• All elements in the array must be of the same data type

An array holding 5 int elements


22 33 66 100 72

An array holding 4 rabbit objects

Reference to Rabit Object


Arrays in Java (Contd…)
• Reference variables are used in Java to store the references of
objects created by the operator new
• Any one of the following syntax can be used to create a reference to
an int array
int x[];
int [] x;

• The reference x can be used for referring to any int array

//Declare a reference to an int array


int [] x;
//Create a new int array and make x refer to it
x = new int[5];
Arrays in Java (Contd…)
• The following statement also creates a new int
array and assigns its reference to x
int [] x = new int[5];

• In simple terms, references can be seen as


names of an array
Initializing Arrays
• An array can be initialized while it is created as
follows:
int [] x = {1, 2, 3, 4};

char [] c = {‘a’, ‘b’, ‘c’};


Length of an Array
• Unlike C, Java checks the boundary of an array
while accessing an element in it
• Programmer not allowed to exceed its boundary
• And so, setting a for loop as follows is very
common:

for(int i = 0; i < x.length; ++i){


x[i] = 5;
}

This works for


use the .length attribute of an array to control the for loop
any size array
Example
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (double element: myList) {//for each loop
System.out.println(element);
}
}
}
Output:
1.9
2.9
3.4
3.5
Can we change the array length?
⚫ int[] primes=new int[10];
……
primes=new int[50];
⚫ Previous array will be discarded
2D Arrays
⚫ Representing 2D arrays
− int myArray[][];
− myArray = new int[3][4];
− int myArray [][] = new int[3][4];
⚫ Example
⚫ int myarray[][]={{0,0,0},{1,1,1}};

2 columns and 3 rows


Multidimensional Arrays
• A Multi-dimensional array is an array of arrays

• To declare a multidimensional array, specify each additional index using


another set of square brackets
• Refer example: ArrayExample.java

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

You might also like