How to call same method from two different classes in java
10,187
is there any better way?
Yes, Refactor that application into a more oop way...
Remember: you cannot Modify Apple or Orange, but OOP allows you extending those classes for sure since they are not final...
class CorrectApple extends Apple implement IJuiceble{
@Override
int getJuice();
}
class CorrectOrange extends Orange implement IJuiceble{
@Override
int getJuice();
}
now the
interface IJuiceble{
int getJuice();
}
and finally the manager:
class FruitManeger {
private CorrectApple apple;
private CorrectOrange orange;
int getJoice(IJuiceble correctfruit){
return correctfruit.getJuice();
}
}
Related videos on Youtube
![max](https://i.stack.imgur.com/A0fvm.png?s=256&g=1)
Author by
max
Updated on June 04, 2022Comments
-
max about 2 years
I have two classes Apple and Orange like this:
public final class Apple{ int getJuice(); } public final class Orange{ int getJuice(); }
and I cannot change them. I have third class fruitsManeger like this:
class FruitManeger { Apple apple; Orange orange; enum Fruits{ Apple,Orange } Fruits favorFruits; int getJoice(){ if(favorFruits==Fruits.Apple){ apple.getJuice(); }else if(favorFruits==Fruits.Orange){ orange.getJuice(); } } }
My Question: what is best way to implement getJuice method in FruitManeger class?.
as you can see if I have a lot of fruits I should add a lot of if else expiration.
of course, I can use reflection to call methods by name but it's not a good idea when getJuice method return an object and you want to do something same with that.
is there any better way?
-
max over 7 yearsi cannot change apple and orange class
-
KC Wong over 7 yearsWe will just have to ask why you cannot change Apple and Orange... because the huge amount of if/case statements you'd need is a direct result of that. By the way you can use Reflection to handle the returned object as well.... you'd just need more if/case statements.
-
max over 7 yearsbecause Apple and orange is in java library
-
ΦXocę 웃 Пepeúpa ツ over 7 yearsApple and Orange are non final classes, extend those then... look my suggested answer please...
-
Darshit over 7 yearsI think you should read about factory design pattern.It is perfect scenario for your question.
-
-
KC Wong over 7 yearsThe question stated, for whatever reason, the classes Apple and Orange cannot be changed, and they don't implement or extend anything.
-
Scary Wombat over 7 yearsa moving target
-
ΦXocę 웃 Пepeúpa ツ over 7 years@max well if you dont provide the correct/complete information then this answer is not even going to compile... and my design is now useless....
-
ΦXocę 웃 Пepeúpa ツ over 7 yearsthe more fruits you have in the manager the bigger the switch case and the enum....
-
jklee over 7 yearsIt's a bad solution, but is a solution for the question. I prefer the interface solution as well.
-
ΦXocę 웃 Пepeúpa ツ over 7 yearsI like the way you approached anyway\
-
Turo over 7 yearsYou could still make a CorrectApple that has an Apple.
-
TimeTraveler over 4 yearsCan we achieve same thing using annotations? That would save redefining all classes?
-
user2441441 over 3 yearsQuestion: Should FruitManager also implement IJuicable as a good practice? Should the class FruitManager be called FruitFactory as a good practice? Should we have a List storing all the fruits in FruitManager so we can easily loop through the fruits?