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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started