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();
     }
}
Share:
10,187

Related videos on Youtube

max
Author by

max

Updated on June 04, 2022

Comments

  • max
    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
      max over 7 years
      i cannot change apple and orange class
    • KC Wong
      KC Wong over 7 years
      We 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
      max over 7 years
      because Apple and orange is in java library
    • ΦXocę 웃 Пepeúpa ツ
      ΦXocę 웃 Пepeúpa ツ over 7 years
      Apple and Orange are non final classes, extend those then... look my suggested answer please...
    • Darshit
      Darshit over 7 years
      I think you should read about factory design pattern.It is perfect scenario for your question.
  • KC Wong
    KC Wong over 7 years
    The question stated, for whatever reason, the classes Apple and Orange cannot be changed, and they don't implement or extend anything.
  • Scary Wombat
    Scary Wombat over 7 years
    a moving target
  • ΦXocę 웃 Пepeúpa ツ
    Φ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 ツ
    ΦXocę 웃 Пepeúpa ツ over 7 years
    the more fruits you have in the manager the bigger the switch case and the enum....
  • jklee
    jklee over 7 years
    It's a bad solution, but is a solution for the question. I prefer the interface solution as well.
  • ΦXocę 웃 Пepeúpa ツ
    ΦXocę 웃 Пepeúpa ツ over 7 years
    I like the way you approached anyway\
  • Turo
    Turo over 7 years
    You could still make a CorrectApple that has an Apple.
  • TimeTraveler
    TimeTraveler over 4 years
    Can we achieve same thing using annotations? That would save redefining all classes?
  • user2441441
    user2441441 over 3 years
    Question: 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?