What is the output of the following Java program? class Food { Food() { System.out.println(flavor); } String flavor = "bland"; } class Pepper extends Food { String flavor = "spicy"; } public class Lunch { public static void main(String[] args) { Food lunch = new Pepper(); } }
Select one: a. spicy b. the program does not compile c. bland spicy d. no output e. bland
What is the output of the following Java program?
class Food {
Food() {
System.out.println(flavor);
}
String flavor = "bland";
}
class Pepper extends Food {
String flavor = "spicy";
}
public class Lunch {
public static void main(String[] args) {
Food lunch = new Pepper();
}
}
Output :--. (b) The program does not compile
Explanation :-
To run the parent class method , you need to create parent class
object Food obj = new Food
. The object type that you
are creating is not of Food Class , it is of Pepper Class.
Get Answers For Free
Most questions answered within 1 hours.