Question: Consider the following classes. public class Student { private String studentName; private int enrollYear; public Student(String name, int enrolled) { studentName = name; enrollYear =

Consider the following classes.

public class Student { private String studentName; private int enrollYear; public Student(String name, int enrolled) { studentName = name; enrollYear = enrolled; } public String toString() { return studentName + ". Enrolled: " + enrollYear; } public int getEnroll() { return enrollYear; } } public class Graduate extends Student { private int gradYear; public Graduate(String name, int enrolled, int graduated) { super(name, enrolled); gradYear = graduated; } public String toString() { return super.toString() + " Graduated: " + gradYear; } public int yearsStudied() { return gradYear - super.getEnroll(); } } 

Consider the following code segment that appears in a class other than Student or Graduate.

1 Student me = new Graduate(Dave, 2008, 2012); 2 System.out.Println(me.toString()); 3 System.out.Println(Years studied:  + me.yearsStudied()); 

When this compilation and execution of this code is attempted, which of the following best describes the results?

Line 2 will cause an error at compilation as there are two methods named toString().

Line 3 will cause an error at compilation as the object me is defined as a Student, but the method yearsStudied() is only in the Graduate class.

The code will compile, and when executed will print the lines Dave. Enrolled: 2008 Graduated: 2012 and Years studied: 4.

The code will compile, and when executed will print the lines Dave. Enrolled: 2008 and Years studied: 4.

The code will compile, and when executed will print the line Dave. Enrolled: 2008 and nothing else.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!