object oriented programming java homework question about abstracting and interfaces
/*
* Driver program that demonstrates the classes ,Fruit.java,
TreeFruit.java, GroudFruit.java
* Apple.java
* Banana.java
* StrawBerry.java
* BlackBerry.java
* */
//Driver.java
import java.util.ArrayList;
public class Driver
{
public static void main(String[] args)
{
ArrayList<Fruit> fruits=new
ArrayList<Fruit>();
fruits.add(new Apple());
fruits.add(new Banana());
fruits.add(new StrawBerry());
fruits.add(new BlackBerry());
//calling method
,prepareFruits
prepareFruits(fruits);
}
/*The method prepareFruits takes ArrayList of Fruit
type
* and then call the peel for the Apple and
Banana
* or pick for straw berry and blue berry
*/
private static void
prepareFruits(ArrayList<Fruit> fruits)
{
for (Fruit fruit : fruits)
{
//check if fruit
is an instance of Apple then call
//peel
method
if(fruit
instanceof Apple)
((Apple) fruit).peel();
//check if fruit
is an instance of Apple then call
//peel
method
else if(fruit
instanceof Banana)
((Banana) fruit).peel();
//check if fruit
is an instance of StrawBerry then call
//pick
method
if(fruit
instanceof StrawBerry)
((StrawBerry) fruit).pick();
//check if fruit
is an instance of BlackBerry then call
//pick
method
else if(fruit
instanceof BlackBerry)
((BlackBerry) fruit).pick();
System.out.println("Vitamins : "+fruit.getVitamin());
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
//Fruit.java
public abstract class Fruit
{
public String color;
public abstract String getVitamin();
}
------------------------------------------------------------------------------------------------------------------------------------------------------
//TreeFruit.java
public interface TreeFruit
{
public void peel();
}
------------------------------------------------------------------------------------------------------------------------------------------------------
//GroundFruit.java
public interface GroundFruit
{
public void pick();
}
------------------------------------------------------------------------------------------------------------------------------------------------------
//Apple.java
public class Apple extends Fruit implements TreeFruit
{
//Override peel method of the TreeFruit
interface
public void peel() {
System.out.println("Peeling an
apple");
}
//Override getVitamin method of the Fruit abstract
class
public String getVitamin() {
return "A B12";
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
//Banana.java
public class Banana extends Fruit implements TreeFruit
{
//Override peel method of the TreeFruit
interface
public void peel() {
System.out.println("Peeling a
banana");
}
//Override getVitamin method of the Fruit abstract
class
public String getVitamin() {
return "C D";
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
//StrawBerry.java
public class StrawBerry extends Fruit implements GroundFruit
{
//Override getVitamin method of the Fruit abstract
class
public String getVitamin() {
return "B5 E";
}
//Override
public void pick() {
System.out.println("Picking a
strawberry");
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
//BlackBerry.java
public class BlackBerry extends Fruit implements GroundFruit
{
//Override getVitamin method of the Fruit abstract
class
public String getVitamin() {
return "C K";
}
//Override
public void pick() {
System.out.println("Picking a
blackberry");
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
object oriented programming java homework question about abstracting and interfaces 2. Definition of a hierarchy of...