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

Topics: Constructors in Java Creating/Instantiating Objects Types of Constructors

This document discusses constructors in Java, including: 1. Constructors help to create and initialize objects. If no constructor is defined, Java provides a default no-argument constructor. 2. There are three types of constructors - no-argument, parameterized, and overloaded constructors. Parameterized constructors allow passing arguments to initialize object attributes. 3. Constructors do not have a return type, not even void. Code within a constructor executes whenever a new object is created.

Uploaded by

Shubhankar Singh
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)
121 views

Topics: Constructors in Java Creating/Instantiating Objects Types of Constructors

This document discusses constructors in Java, including: 1. Constructors help to create and initialize objects. If no constructor is defined, Java provides a default no-argument constructor. 2. There are three types of constructors - no-argument, parameterized, and overloaded constructors. Parameterized constructors allow passing arguments to initialize object attributes. 3. Constructors do not have a return type, not even void. Code within a constructor executes whenever a new object is created.

Uploaded by

Shubhankar Singh
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

Topics

• Constructors in Java
• Creating/Instantiating Objects
• Types of Constructors

1 Object-Oriented Programming Using Java


Role of Constructor Method

• Constructor is a Method of a class having a name similar to


the class itself
• Helps to Create and Initialize the Objects
• If no constructor is defined for the class then a default un-
parameterized constructor will be provided by Java Runtime
Environment.
• Constructor Method has no return type not even void.
• Code written inside constructor method is automatically
executed for every object creation of that class.
• If a class has its own constructor then default constructor will
not be available
• Constructor Method can have its own Access Modifier :
public, protected, package-private and private
2 Object-Oriented Programming Using Java
Object Creation Syntax

• Creating/Instantiating Objects
<Class-Name> object-reference = new Constructor();
new operator used to create
objects at runtime
Examples
Box b1 = new Box();

String s1 = new String();

Class Name Object-Reference Names Names of Constructor Methods

Note : Above Examples Use Unparametrized Constructor

3 Object-Oriented Programming Using Java


Object Creation Example
/* File Name : Box.java */
class Box
{
private double length; // length of a Box
private double width; // width of a Box
private double height; // height of a Box

/* Method to Compute the Area of Box */


Class Box Has No
public
{
double area()
Constructor
return 2* (length * width + width * height + height * length);
} // End of Method

/* Method to Compute the Volume of Box */


public double volume()
{
return length * width * height;
} // End of Method
}// End of class Box

class BoxTest
{
public static void main (String [ ] args)
{ Constructor
Box b1 = new Box();
Box b2 = new Box(); Provided by
}// End of Method
}// End of class BoxTest JRE
4 Object-Oriented Programming Using Java
Types of Constructors

1. Un-parameterized Constructor
2. Parameterized Constructor
3. Overloaded Constructors

5 Object-Oriented Programming Using Java


Class With Unparametrized
Constructor
// File Name : Demo.java
class A
{
private int a;
private int b;
A()
{ Un-parameterized
System.out.println(“Hello Welcome”);
}
Constructor
}// End of class A :A
class ConstructorDemo
{ a1 a b
public static void main(String args[])
{
a3
A a1 = new A(); :A
A a2 = new A(); a2 a b
A a3 = a1;
A a4 = a2; a4
} // End of Method
}// End of class ConstructorDemo Only Two Objects are Created in This Example
6 Object-Oriented Programming Using Java
Class With Unparametrized
Constructor
// File Name : Demo.java
class A
{
private int a;
private int b; Predict the O/P of This
A()
{
Code
System.out.println(“Hello Welcome”);
}
}// End of class A
class ConstructorDemo
{
public static void main(String args[])
{
A a1 = new A(); Output
A a2 = new A(); F:\>javac Demo.java
F:\>java ConstructorDemo
A a3 = a1;
Hello Welcome
A a4 = a2; Hello Welcome
} // End of Method
}// End of class ConstructorDemo
7 Object-Oriented Programming Using Java
Class With Unparametrized
Constructor
// File Name : Demo.java
class A
{
private int a;
private int b;
A()
{
for(int i =0; i<10; i++)
System.out.println (“Hello”); Predict The Output
}
}// End of class A
class ConstructorDemo
{
public static void main(String args[])
{
A a1 = new A();
A a2 = new A();
A a3 = new A();
A a4 = new A();
} // End of Method
}// End of class ConstructorDemo
8 Object-Oriented Programming Using Java
Class With Parameterized
Constructor
// File Name : Demo.java
class A
{
private int a;
private int b;

A(int a, int b)
{
this.a = a; Parameterized
this.b = b;
displayValues(); Constructor
} // End of Constructor Method

/* Method to Display the Attribute Values */


public void displayValues()
{
System.out.println(“ a = “ + a);
System.out.println(“ b = “ + b); Compile-Time Error
}// End of Method
}// End of class A F:\>javac Demo.java
class ConstructorDemo Demo.java:27: cannot find symbol
{
public static void main(String args[])
symbol : constructor A()
{ location: class A
A a1 = new A();
A a1 = new A();
^
} // End of Method
}// End of class ConstructorDemo
1 error
9 Object-Oriented Programming Using Java
Class With Parameterized
Constructor
// File Name : Demo.java
class A
{
private int a;
:A
private int b;
a1 20 40
A(int a, int b)
{ a b
this.a = a;
this.b = b;
:A
displayValues();
} // End of Constructor Method a2 10 6
/* Method to Display the Attribute Values */ a b
public void displayValues()
{
System.out.println(“ a = “ + a);
System.out.println(“ b = “ + b);
}// End of Method
}// End of class A Output
class ConstructorDemo F:\>javac Demo.java
{
public static void main(String args[])
F:\>java ConstructorDemo
{ a = 20
A a1 = new A(20,40); b = 40
A a2 = new A(10,6); a = 10
} // End of Method
}// End of class ConstructorDemo b=6
10 Object-Oriented Programming Using Java
Class with Overloaded
Constructors
• Constructor Methods are Overloaded if they have same name but different signatures
// File Name : Demo.java // Method to print the sides of Triangle
class Triangle public void displaySides()
{ {
private double a; // Side 1 of Triangle System.out.println("Side 1:" + a);
private double b; // Side 2 of Triangle System.out.println("Side 2:" + b);
private double c; // Side 2 of Triangle System.out.println("Side 3:" + c);
}// End of Method
// Constructor Method - 1 Used to Create Equilateral Triangle }// End of Class
Triangle(double side)
{ /* Driver Class */
a = b = c = side; class ConstructorDemo
displaySides(); {
}// End of Constructor public static void main(String args[])
// Constructor Method - 1 Used to Create Isosceles Triangle {
Triangle(double side1, double side2)
{ Triangle T1 = new Triangle(10);
a = b = side1; c = side2;
displaySides(); Triangle T2 = new Triangle(20, 30);
}// End of Constructor Triangle T3 = new Triangle(10,6,8);
// Constructor Method - 3 Used to Create Scalene Triangle
Triangle(double side1, double side2, double side3) } // End of Method
{ }// End of class ConstructorDemo
a = side1; b = side2; c = side3;
displaySides();
}// End of Constructor

11 Object-Oriented Programming Using Java


Class with Overloaded
Constructors
• Output of the Java Program of Previous Slide

F:\>javac Demo.java

F:\>java ConstructorDemo
Side 1:10.0
Side 2:10.0
Side 3:10.0
Side 1:20.0
Side 2:20.0
Side 3:30.0
Side 1:10.0
Side 2:6.0
Side 3:8.0

12 Object-Oriented Programming Using Java


Constructors With Access
Modifiers
// File Name : Demo.java Output
class XYZ
{
private int x, y, z; F:\>javac Demo.java
// Constructor with ‘private’ Modifier Demo.java:25: cannot find symbol
private XYZ (int a) symbol : constructor A(int)
{
x = a; location: class A
} A a1 = new A(10);
// Constructor with public Modifier ^
public XYZ (int y, int z) 1 error
{
this.y = y;
this.z = z;
}
}// End of class XYZ
// Driver Class
class Test
{
// Driver Method
Compile-Time
public static void main(String args[])
{
Error
A a1 = new A(10);
A a2 = new A(10,40);
} // End of Method
} // End of class Test
13 Object-Oriented Programming Using Java
Constructor Summary

• Method of a class having same name as that of class


• No return type (not even void) for Constructor Methods
• Can have access Specifiers (Modifiers)

14 Object-Oriented Programming Using Java


Thank You

15 Object-Oriented Programming Using Java

You might also like