0% found this document useful (0 votes)
40 views15 pages

Constructors and Method Overloading 2022

1) Constructors initialize objects by setting default values for data fields when an object is created. 2) A constructor has the same name as its class and is invoked automatically at object creation. 3) Multiple constructors can exist to allow objects to be initialized in different ways depending on parameters passed. This allows objects to be initialized with different sets of initial values.

Uploaded by

anjana dilan
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)
40 views15 pages

Constructors and Method Overloading 2022

1) Constructors initialize objects by setting default values for data fields when an object is created. 2) A constructor has the same name as its class and is invoked automatically at object creation. 3) Multiple constructors can exist to allow objects to be initialized in different ways depending on parameters passed. This allows objects to be initialized with different sets of initial values.

Uploaded by

anjana dilan
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/ 15

Classes and Objects in Java

Constructors, Method
Overloading

1
Object Initialisation
 When objects are created, the initial value of data fields is
unknown unless its users explicitly do so.
 In many cases, it makes sense if this initialisation can be
carried out by default without the users explicitly initializing
them.
 In Java, this can be achieved though a mechanism called
constructors.

2
What is a Constructor?
 Constructor is a special method that gets invoked
“automatically” at the time of object creation.

 Constructor is normally used for initializing objects with


default values unless different values are supplied.

 Constructor has the same name as the class name.

 Constructor cannot return values.

 A class can have more than one constructor as long as


they have different signature (constructor name
followed by the parameter list).

3
Defining a Constructor
 Like any other method
public class ClassName {

// Data Fields…
// Constructor
public ClassName()
{
// Method Body Statements initialising Data Fields
}
//Methods to manipulate data fields
}

 Invoking:
 When the object creation statement is executed, the
constructor method will be executed automatically.

4
Defining a Constructor: Example
public class Counter {
int CounterIndex;

// Constructor
public Counter()
{
CounterIndex = 0;
}
//Methods to update or access counter
public void increase()
{
CounterIndex = CounterIndex + 1;
}
public void decrease()
{
CounterIndex = CounterIndex - 1;
}
int getCounterIndex()
{
return CounterIndex;
}
}
5
Trace counter value at each statement and
What is the output ?
public class Counter {
int CounterIndex;

class MyClass { // Constructor


public static void main(String args[]) public Counter()
{
{ CounterIndex = 0;
int a b; }
//Methods to update or access counter
Counter counter1 = new Counter(); public void increase()
counter1.increase(); {
CounterIndex = CounterIndex + 1;
a = counter1.getCounterIndex(); }
counter1.increase(); public void decrease()
{
b = counter1.getCounterIndex(); CounterIndex = CounterIndex - 1;
if ( a > b ) }
int getCounterIndex()
counter1.increase(); {
else return CounterIndex;
}
counter1.decrease(); }

System.out.println(counter1.getCounterIndex());
} public Counter(int InitValue )
} {
CounterIndex = InitValue;
} 6
A Counter with User Supplied Initial Value ?
 This can be done by adding another constructor
method to the class.
public class Counter {
int CounterIndex;

// Constructor 1
public Counter()
{
CounterIndex = 0;
}
public Counter(int InitValue )
{
CounterIndex = InitValue;
}
}

// A New User Class: Utilising both constructors


Counter counter1 = new Counter();
Counter counter2 = new Counter (10);

7
Adding a Multiple-Parameters Constructor to
our Circle Class

public class Circle {


public double x,y,r;
// Constructor
public Circle(double centreX, double centreY,
double radius)
{
x = centreX;
y = centreY;
r = radius;
}
//Methods to return circumference and area
public double circumference() { return 2*3.14*r; }
public double area() { return 3.14 * r * r; }
}

8
Constructors initialise Objects

 Recall the following OLD Code Segment:


Circle aCircle = new Circle();
aCircle.x = 10.0; // initialize center and radius
aCircle.y = 20.0
aCircle.r = 5.0;
aCircle = new Circle() ;
At creation time the center and
radius are not defined.

These values are explicitly set later.

9
Constructors initialise Objects

 With defined constructor


Circle aCircle = new Circle(10.0, 20.0, 5.0);

aCircle = new Circle(10.0, 20.0, 5.0) ;

aCircle is created with center (10, 20)


and radius 5

10
Multiple Constructors

 Sometimes want to initialize in a number


of different ways, depending on
circumstance.

 This can be supported by having multiple


constructors having different input
arguments.

11
Multiple Constructors
public class Circle {
public double x,y,r; //instance variables
// Constructors
public Circle(double centreX, double cenreY, double radius) {
x = centreX; y = centreY; r = radius;
}
public Circle(double radius) { x=0; y=0; r = radius; }
public Circle() { x=0; y=0; r=1.0; }

//Methods to return circumference and area


public double circumference() { return 2*3.14*r; }
public double area() { return 3.14 * r * r; }
}

12
Initializing with constructors
public class TestCircles {

public static void main(String args[]){


Circle circleA = new Circle( 10.0, 12.0, 20.0);
Circle circleB = new Circle(10.0);
Circle circleC = new Circle();
}
}

circleA = new Circle(10, 12, 20) circleB = new Circle(10) circleC = new Circle()

Centre = (10,12) Centre = (0,0)


Radius = 20 Centre = (0,0) Radius = 1
Radius=10 13
Method Overloading

 Constructors all have the same name.


 Methods are distinguished by their signature:

 name
 number of arguments
 type of arguments
 position of arguments

 That means, a class can also have multiple


usual methods with the same name.

14
15

You might also like