Predict the output of the following Java program: 1 class Fruit { 2 protected String fruitName; 3 public void setName(String name) { 4 fruitName = name; 5 } 6 public String toString() { 7 return fruitName; 8 } 9 } 10 class Apple extends Fruit { 11 private String color; 12 public Apple(String colorName) { 13 color = colorName; 14 } 15 public String toString() { 16 return super.toString() + ("" + color + ""); 17 } 18 } 19 public class Main { 20 public static void main(String args[]) { 21 Apple myApple = new Apple("Red"); 22 myApple.setName("Apple"); 23 System.out.println(myApple); 24 } 25 }
Added by Tonya A.
Close
Step 1
The toString method is overridden to return the value of fruitName. Show more…
Show all steps
Your feedback will help us improve your experience
Akash M and 95 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Answer the following question using these classes and code: public class Mood { public void saySomething() { System.out.print("What?"); } } public class Meh extends Mood { public void saySomething() { System.out.print("Meh"); } } public class Fine extends Meh { } public class Happy extends Fine { public void saySomething() { System.out.print("Almost done!"); } } public class Joy extends Happy { } // Declarations in code preceding the questions Fine fine = new Fine(); Happy happy = new Happy(); Joy joy = new Joy(); What is the output for the statement: joy.saySomething()?
Akash M.
A cartoon contains 7 red and 5 green apples. 3 apples are drawn at random. Find the probability that they are of the same colors.
Donna D.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD