Having overload methods from one abstract method in JAVA

Given an abstract class

public abstract class AbstractStomach {
    public abstract void consume(Food food);
}

I want to have some specific classes with various overload methods:

public class FruitStomach extends AbstractStomach {
    public static class Apple extends Food {}
    public static class Orange extends Food {}
    @Override
    public void consume(Apple food) {...}
    @Override
    public void consume(Orange food) {...}
}

public class MeatStomach extends AbstractStomach {
    public static class Beef extends Food {}
    @Override
    public void consume(Beef food) {...}
}

The above does not work, because it forces me to implement also a general abstract method consume(Food)in each concrete class.

But I don’t want concrete classes to implement consume(Food), because they took into account their overload options, and indeed, each particular class should consume only different and specific foods, and not ANY food, so that

AbstractStomach stomach = new FruitStomach();
stomach.consume(new Beef());

should give an error.

If I delete an abstract method in an abstract class, then it makes no sense to extend it with an abstract class, since it does not give them any guidance on what to implement.

, , , consume() , consume(Food).


, ,

public class FruitStomach extends AbstractStomach {
    public static class Apple extends Food {}
    public static class Orange extends Food {}
    @Override
    public void consume(Food food) {
        if (food instanceof Apple) {
           ...
        } else if (food instanceof Orange) {
           ...
        } else {
           // throws an error
        }
    }
}

, . , .

+4
5

- , " ". , . , ,

public void evaluateDigestion(AbstractStomach stomach) {
}

Stomach, a FruitStomach MeatStomach DessertStomach . evaluateDigestion stomach.consume(someKindOfFood). consume, ( evaluateDigestion , ), consume, Food. consume "" consume(Food food), . consume Food. Apple, Orange ..; Java . ( , , " ", , , .)

,

public void consume(Food food)

.

public void consume(Food food) {
    if (food instanceof Apple) {
        consume((Apple)food);  // calls the overloaded version because you've use a cast to tell Java how to view the object
    } else if (food instanceof Orange) {
        consume((Orange)food);
    } else {
        throw ... // some exception that occurs when you try to eat the wrong kind of food
    }
}

Food, , Food :

public abstract class Food {
    public void abstract consumedBy(AbstractStomach stomach);
}

Apple Orange .., consume

public static void consume(Food food) {
    food.consumedBy(this);
}

FruitStomach . , instanceof, , . ( : Fruit, Food, Apple Orange Fruit, food instanceof Fruit, , . .)

+3

generics () :

AbstractStomach T, , Food :

public abstract class AbstractStomach<T extends Food> {
    public abstract void consume(T food);
}

Fruit FruitStomach:

public class Fruit extends Food {}
public class FruitStomach extends AbstractStomach<Fruit> {
    public static class Apple extends Fruit {}
    public static class Orange extends Fruit {}
    @Override
    public void consume(Fruit food) {}
}

Meat MeatStomach:

public class Meat extends Food {}
public class MeatStomach extends AbstractStomach<Meat> {
    public static class Beef extends Meat {}
    @Override
    public void consume(Meat food) {}
}
+3

@ , , , " " " ".

, , , - , (, , instanceof). , , :

Food orange = new Orange();
Food apple = new Apple(); 

Orange Apple.

, , , , - .

+1
The above doesn't work as it is forcing me to implement also the generic consume(Food)

right. AbstractStomach ,

, AbstractStomach, consume(Food) ( ), , .

+1

, , , , . (, , ) .

.

   public FruitStomach extends AbstractStomach {
    public static class Apple extends Food {}
    public static class Orange extends Food {}
    @Override
    public void consume(Food food) {
      if (food instanceof Orange) {
        Orange orange = (Orange) food;
        // do smth. with orange     
      } else{
        // so smth with food
      }
    }

   public MeatStomach extends AbstractStomach {
      public static class Beef extends Food {}
      @Override
      public void consume(Food food) {
        Beef beef = (Beef) food;
        // do smth. with beef...
      }
    }

. , , . , , Stomach.consume() , ClassCastException, .

+1

Source: https://habr.com/ru/post/1668483/


All Articles