Given the following class class Main { private static class Vegetable { } privat
ID: 3781422 • Letter: G
Question
Given the following class
class Main {
private static class Vegetable {
}
private static class Carrot extends Vegetable {
}
public static void main(String[] args) {
List<Potato> potatos = new ArrayList<>();
eatVegetable(potatos);
}
private static void eatVegetable(List<Vegetable> fruit) {
}
}
- Please explain why the following code will not compile.
- What change to the signature of the ‘eatVegetable’ method will allow it to accept lists of any types of vegetable (but only lists of type vegetable).
Explanation / Answer
The above code will not be executed as this code is wrongly written.
The class main should be writtem before void main()..
And also there are two classes here vegetable and class.
Here class vegetable is inherited to the class carrot. But here these two classes are written as private. As the private specifier is used before any class that class methods and variables will not extend to other class. Because of all these errors this code will not compile untill we made the changes.
--> change the private keyword to the public in eatvegatable method also as main method is accessing this eatvegatable method to access the vegatable from list.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.