Question: Write a program named VegeFamily.java , where you will define a few classes: A Vegetable class that has a constructor (printing out I am a
Write a program named VegeFamily.java, where you will define a few classes:
A Vegetable class that has a constructor (printing out I am a vege!) and a dispose method (printing out Vege is consumed!)
A Tomato class that extends Vegetable, and has a constructor (printing out I am a tomato!) and a dispose method (printing out A tomato is consumed! and calling its superclasss dispose method)
A Lettuce class and a Cabbage class, defined similarly to the Tomato class.
The VegeFamily class itself, whose members include an array of 3 vegetables initialized with a tomato, a lettuce and a cabbage. It also has a dispose method that invokes the dispose methods of all the vegetables in the array. In the main method, create a VegeFamily object and call its dispose method.
Based on the initialization and cleanup order, the program should work like below:
> I am a vege!
> I am a tomato!
> I am a vege!
> I am a lettuce!
> I am a vege!
> I am a cabbage!
> A tomato is consumed!
> Vege is consumed!
> A lettuce is consumed!
> Vege is consumed!
> A cabbage is consumed!
> Vege is consumed!
Hint: The point here is to demonstrate that the cleanup is done in the reverse order of the initialization.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
