EXPERIMENT 1.
Student Name: SANT PRASAD PATHAK UID : 20BET1103
Branch: IT Section/Group:BET-1 (B)
Semester: 3 Subject Name : JAVA PROGRAMMING
Q1- Write a program to study different types of constructors in java.
AIM : To understand about default constructors
CODE:
class Demo{
int value1;
int value2;
//inialization of instance variable in the default constructor of class
Demo(){
value1 = 10;
value2 = 20;
System.out.println("Inside Constructor");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
}
public static void main(String args[]){
Demo d1 = new Demo();
d1.display();
}
}
Observation :
Here in this code I uderstood the few concepts such as what is default constructor that it is
refers to a nullary constructor that is automatically generated by the compiler if no constructors
have been defined for the class. The default constructor implicitly calls the superclass's nullary
constructor, then executes an empty body.
Output:
AIM : To understand about parameterized constructors
CODE:
class Demo{
int value1;
int value2;
Demo(){
value1 = 0;
value2 = 0;
System.out.println("Inside 1st Constructor");
}
Demo(int a)
{ value1 =
a;
System.out.println("Inside 2nd Constructor");
}
Demo(int a,int b)
{ value1 = a;
value2 = b;
System.out.println("Inside 3rd Constructor");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
}
public static void main(String args[]){
Demo d1 = new Demo();
Demo d2 = new Demo(30);
Demo d3 = new Demo(30,40);
d1.display();
d2.display();
d3.display();
}
}
Output:
AIM : To understand about constructors chaining
Task to be done : Displaying the values of variable under the concept of
constructors chaining
CODE:
class Demo{
int value1;
int value2;
Demo(){
value1 = 1;
value2 = 2;
System.out.println("Inside 1st Parent Constructor");
}
Demo(int a){
value1 = a;
System.out.println("Inside 2nd Parent Constructor");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
}
public static void main(String args[])
{ DemoChild d1 = new
DemoChild(); d1.display();
}
}
class DemoChild extends Demo{
int value3;
int value4;
DemoChild(){
//super(5);
value3 = 3;
value4 = 4;
System.out.println("Inside the Constructor of Child");
}
public void display(){
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
System.out.println("Value1 === "+value3);
System.out.println("Value2 === "+value4);
}
}
Output:
Learning outcomes (What I have learnt):
1. understood concept of default const.
2. understood concept of parameterize const.
3. understood concept of const. chaining
Evaluation Grid:
Sr. No. Parameters Marks Obtained Maximum Marks
1. Worksheet completion including writing 10
learning objectives/Outcomes.(To be
submitted at the end of the day).
2. Post Lab Quiz Result. 5
3. Student Engagement in 5
Simulation/Demonstration/Performanc
e and Controls/Pre-Lab Questions.
Signature of Faculty (with Date): Total Marks Obtained: