2(ix)
Consider the given program and answer the questions given below:
class temp
{
int a;
temp()
{
a=10;
}
temp(int z)
{
a=z;
}
void print()
{
System.out.println(a);
}
void main()
{
temp t = new temp();
temp x = new temp(30);
t.print();
x.print();
}
}
(a) What concept of OOPs is depicted in the above program with two constructors?
(b) What is the output of the method main()?
Show SolutionHide Solution↓
(a) The concept of Constructor Overloading (Polymorphism) is depicted in the program.
(b) Output of the main() method:
10
30
Explanation:
Output of the program is explained below:
temp t = new temp();
Calls the default constructortemp()→a = 10temp x = new temp(30);
Calls the parameterized constructortemp(int z)→a = 30t.print();
Prints value ofafort→10x.print();
Prints value ofaforx→30