Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA PROGRAMMING: Look at the following code and then answer the questions from

ID: 3914407 • Letter: J

Question

JAVA PROGRAMMING:

Look at the following code and then answer the questions from Q1 to Q3

//Person.java

public abstract class Person {

protected String person_name;

protected int age;

private String city;

public Person() {

System.out.println("Constructor of Person class"); }

public void set_name(String name) {

person_name = name ; }

public String get_name() {

return person_name; }

public void set_age(int p_age) {

age = p_age; }

public int get_age() {

return age; }

public void my_method() {

System.out.println("I need to focus during the exam"); }

public abstract void display_information();

public abstract void do_something();}

//Employee.java

public class Employee extends Person{

private long employee_id = 531231;

public Employee() {

System.out.println("Constructor of Employee class"); }

public void set_id(long id) {

employee_id = id; }

public long get_id () {

return employee_id; }

@Override

public void display_information() {

System.out.println("Name: " + person_name);

System.out.println("Age: " + age);

System.out.println("ID: " + employee_id); }

@Override

public void do_something() {

System.out.println("I am an Employee"); }

}

//Student.java

public class Student extends Person {

private long student_id = 877552;

public Student() {

System.out.println("Constructor of Student class"); }

public void set_student_id(long id) {

student_id = id; }

public long get_student_id() {

return student_id; }

@Override

public void display_information() {

System.out.println("Name: " + person_name);

System.out.println("Age: " + age);

System.out.println("ID: " + student_id); }

@Override

public void do_something() {

System.out.println("I am a student"); }

}

Q1. What is the output of the following program?

// Program 1.java

//TestPerson

public class TestPerson {

public static void main(String[] args) {

Employee e1 = new Employee();

Student p1 = new Student(); }

}

Q2. Does the program below compile? If not, why?

// Program 2

//TestPerson.java

public class TestPerson {

public static void main(String[] args) {

Person p1 = new Person(); }

}

Q3. What is the output of the following program?

// Program 3

//TestPerson

public class TestPerson {

public static void main(String[] args) {

Employee e1 = new Employee();

e1.display_information(); }

}

Explanation / Answer

Answer :

1) Output of 1st program as follows.

Constructor of Person class
Constructor of Employee class
Constructor of Person class
Constructor of Student class

2)

Program 2 does not compile. Error is "Cannot instantiate the type Person"

Abstract modifier are used with properties, classes, methods. The name abstract means class is treated as base class for other classes. and it does not have a complete implementation about class. An abstract class cannot be instantiated. Abstract class contains abstract method and accessors.

3) Output of 3rd program as follows.

Constructor of Person class
Constructor of Employee class
Name: null
Age: 0
ID: 531231