Padre Conceição College Of Engineering
DATE: 05/01/2025 EXPERIMENT NO: 03
AIM: Java program to implement constructor.
THEORY:
Constructor:These are special methods used to intialise objects.
Default Constructor(no argument):The default constructor in Java is a constructor that does not
take any parameters and initializes an object with default values.
Syntax of Default Constructor
class ClassName {
// Default constructor
ClassName() {
// Initialization code
}
}
Parameterized Constructor (Single Argument):A parameterized constructor is a constructor that
takes arguments to initialize an object with specific values.
Syntax
class ClassName {
// Parameterized constructor
ClassName(datatype param1, datatype param2, ...) {
// Initialization code
}
}
Method overloading is demonstrated with three constructors.
DEPARTMENT OF ELECTRONICS AND COMPUTER ENGINEERING, JAVA PROGRAMMING
PAGE NO. : 1 ROLL NO. : 23EC20
Padre Conceição College Of Engineering
SOURCE CODE:
import [Link].*;
public class Boxx {
double width, length, height;
public Boxx()
{
width = 20;
height = 40;
length = 60;
}
public Boxx(double side)
{
length = width = height = side;
}
public Boxx (double w, double h, double l)
{
width = w;
height = h;
length = l;
}
void volume()
{
double vol ;
vol = length * width * height;
[Link]("Volume of the Box : "+vol);
}
}
class Main
{
public static void main (String[]args)
DEPARTMENT OF ELECTRONICS AND COMPUTER ENGINEERING, JAVA PROGRAMMING
PAGE NO. : 2 ROLL NO. : 23EC20
Padre Conceição College Of Engineering
{
double side,w,h,l;
Boxx obj= new Boxx();
[Link]();
Scanner obj1 = new Scanner ([Link]);
[Link]("Enter side of the cube: ");
side=[Link]();
Boxx obj2= new Boxx();
[Link]();
System .[Link]("Enter width, height, length of the cube");
w=[Link]();
h=[Link]();
l=[Link]();
Boxx obj3= new Boxx(w,h,l);
[Link]();
}
}
DEPARTMENT OF ELECTRONICS AND COMPUTER ENGINEERING, JAVA PROGRAMMING
PAGE NO. : 3 ROLL NO. : 23EC20
Padre Conceição College Of Engineering
OUTPUT:
CONCLUSISON: Java Program to Implement Constructor is studied and implemented successfully.
DEPARTMENT OF ELECTRONICS AND COMPUTER ENGINEERING, JAVA PROGRAMMING
PAGE NO. : 4 ROLL NO. : 23EC20