Experiment 1.1: Write A Program To Study Different Types of Constructors in Java
Experiment 1.1: Write A Program To Study Different Types of Constructors in Java
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
have been defined for the class. The default constructor implicitly calls the superclass's nullary
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:
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.
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: