Home »
Java »
Java find output programs
Java find output programs (Constructor & Destructor) | set 3
Find the output of Java programs | Constructor & Destructor | Set 3: Enhance the knowledge of Java Constructor & Destructor concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 03, 2021
Question 1:
class Student {
private int sid;
private String name;
private int fees;
Student() {
sid = 101;
name = "virat";
fees = 8000;
}
Student(int id, String na, int fee) {
sid = id;
name = na;
fees = fee;
}
public void setInfo(int id, String na, int fee) {
sid = id;
name = na;
fees = fee;
}
public void printInfo() {
System.out.println("S-ID: " + sid);
System.out.println("Name: " + name);
System.out.println("Fees: " + fees + "\n");
}
}
public class CtorEx {
public static void main(String[] args) {
Object O1 = new Student();
Object O2 = new Student(102, "Rohit", 5000);
(Student)(O1).printInfo();
(Student)(O2).printInfo();
}
}
Output:
/CtorEx.java:36: error: not a statement
(Student)(O1).printInfo();
^
/CtorEx.java:37: error: not a statement
(Student)(O2).printInfo();
^
2 errors
Explanation:
The above program will generate syntax errors because of the below statements,
Object O1 = new Student();
Object O2 = new Student(102,"Rohit",5000);
(Student)(O1).printInfo();
(Student)(O2).printInfo();
In the above code we did not typecast objects properly. The correct way is given below,
Student S1 = (Student)O1;
Student S2 = (Student)O2;
S1.printInfo();
S2.printInfo();
Question 2:
class Student {
private int sid;
private String name;
private int fees;
Student() {
sid = 101;
name = "virat";
fees = 8000;
}
Student(int id, String na, int fee) {
sid = id;
name = na;
fees = fee;
}
public void setInfo(int id, String na, int fee) {
sid = id;
name = na;
fees = fee;
}
public void printInfo() {
System.out.println("S-ID: " + sid);
System.out.println("Name: " + name);
System.out.println("Fees: " + fees + "\n");
}
}
public class CtorEx {
public static void main(String[] args) {
Student S[] = {
new Student(100, "Jaspreet", 5000),
new Student(101, "Kuldeep", 7000)
};
S[0].printInfo();
S[1].printInfo();
}
}
Output:
S-ID: 100
Name: Jaspreet
Fees: 5000
S-ID: 101
Name: Kuldeep
Fees: 7000
Explanation:
In the above program, we created a class Student that contains data member sid, name, and fees. Here, we defined default and parameterized constructor to initialize the data members, and we also defined setInfo() and printInfo() method to set and print the values of data members.
Now look to the CtorEx class, the CtorEx class contains the main() method, which is the entry program of the program. Here, we created an object array of Student class and initialized with the parameterized constructor and then we print student detail on the console screen.
Question 3:
class Student {
private int sid;
private String name;
private int fees;
Student() {
sid = 101;
name = "virat";
fees = 8000;
}
Student(int id, String na, int fee) {
sid = id;
name = na;
fees = fee;
}
public void setInfo(int id, String na, int fee) {
sid = id;
name = na;
fees = fee;
}
public void printInfo() {
System.out.println("S-ID: " + sid);
System.out.println("Name: " + name);
System.out.println("Fees: " + fees + "\n");
}
}
public class CtorEx {
public static void main(String[] args) {
Student S[] = {
new Student(),
new Student(102, "Kuldeep", 7000)
};
S[0].printInfo();
S[1].printInfo();
}
}
Output:
S-ID: 101
Name: virat
Fees: 8000
S-ID: 102
Name: Kuldeep
Fees: 7000
Explanation:
In the above program, we created a class Student that contains data member sid, name, and fees. Here, we defined default and parameterized constructor to initialize the data members, and we also defined setInfo() and printInfo() method to set and print the values of data members.
Now look to the CtorEx class, the CtorEx class contains the main() method, which is the entry program of the program. Here, we created an object array of Student class and called no-argument constructor with object S[0] and called parameterized constructor with object S[1]. Then finally called the printInfo() method to print student details on the console screen.
Question 4:
class Student {
private int sid;
private String name;
private int fees;
Student() {
sid = 101;
name = "virat";
fees = 8000;
}
Student(int id, String na, int fee) {
sid = id;
name = na;
fees = fee;
}
public void setInfo(int id, String na, int fee) {
sid = id;
name = na;
fees = fee;
}
public void printInfo() {
System.out.println("S-ID: " + sid);
System.out.println("Name: " + name);
System.out.println("Fees: " + fees + "\n");
}
public~Student() {
System.out.println("Destructor called");
}
}
public class CtorEx {
public static void main(String[] args) {
Student S[] = {
new Student(),
new Student(102, "Kuldeep", 7000)
};
S[0].printInfo();
S[1].printInfo();
}
}
Output:
/CtorEx.java:30: error: illegal start of type
public~Student() {
^
1 error
Explanation:
The above program will generate syntax errors because of the below method,
public ~Student()
{
System.out.println("Destructor called");
}
The above method looks like a destructor in C++, but java does not support destructors in Java.
Question 5:
class Student {
private int sid;
private String name;
private int fees;
Student() {
sid = 101;
name = "virat";
fees = 8000;
}
final Student(int id, String na, int fee) {
sid = id;
name = na;
fees = fee;
}
public void setInfo(int id, String na, int fee) {
sid = id;
name = na;
fees = fee;
}
public void printInfo() {
System.out.println("S-ID: " + sid);
System.out.println("Name: " + name);
System.out.println("Fees: " + fees + "\n");
}
}
public class CtorEx {
public static void main(String[] args) {
Student S[] = {
new Student(),
new Student(102, "Kuldeep", 7000)
};
S[0].printInfo();
S[1].printInfo();
}
}
Output:
/CtorEx.java:12: error: modifier final not allowed here
final Student(int id, String na, int fee) {
^
1 error
Explanation:
The above program generates syntax error because we used the final keyword with a parameterized constructor. The final keyword is not allowed with constructors in Java.