KnowledgeBoat Logo

Computer Applications

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()?

Java Constructors

ICSE 2024

8 Likes

Answer

(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:

  1. temp t = new temp();
    Calls the default constructor temp()a = 10

  2. temp x = new temp(30);
    Calls the parameterized constructor temp(int z)a = 30

  3. t.print();
    Prints value of a for t10

  4. x.print();
    Prints value of a for x30

Answered By

5 Likes


Related Questions