Question
11.18 Given the following code, find the compile error? publicclassTest{ public static void main(String[] args) { m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); }
11.18Given the following code, find the compile error?
publicclassTest{
public static void main(String[] args) {
m(new GraduateStudent());
m(new Student());
m(new Person());
m(new Object());
}
public static void m(Student x) {
System.out.println(x.toString());
}
}
classGraduateStudentextendsStudent{
}
classStudentextendsPerson{
public String toString() {
return "Student";
}
}
classPersonextendsObject{
public String toString() {
return "Person";
}
}
A. m(new GraduateStudent()) causes an error
B. m(new Student()) causes an error
C. m(new Person()) causes an error
D. m(new Object()) causes an error
What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but are accessible to any subclasses in any package?
A. public
B. private
C. protected
D. Use the default modifier.
Which of the following statements is false?
A. A public class can be accessed by a class from a different package.
B. A private method cannot be accessed by a class in a different package.
C. A protected method can be accessed by a subclass in a different package.
D. A method with no visibility modifier can be accessed by a class in a different package.
Part b
Consider the following idea. We want to create classes to represent different types of fruit. Specifically, we are interested in making classes for apples and oranges. We know that these classes could share some data, as well as some methods. For instance, both an apple and an orange would have a weight data field, measured in ounces. Both types of fruit could have a color data field in the form of a String. They could both have a data field that stores the amount of calories. They could each have a method that prints the color of the fruit.
Given this description, the possibility of inheritance might pop into your head. However, we know that an orange is not an apple, and an apple is not an orange. What might be a good way to use inheritance to solve this problem? For this exercise, I want you to 'create an abstract class called Fruit. We want this class to be abstract as we do not want to actually create instances of the Fruit class. Instead, we want to use it as a superclass to extend to the Apple and Orange classes.
What will you need in your Fruit class? I suggested some data fields and methods above. Can you think of anymore (Write out your Fruit class definition below)? Include a 'calories' data field and an abstract method to print the color of the fruit.
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