0

I have 2 classes that extended from same parent class. In these classes I have method with same name but different implementation. Is there any way to call this specific method from its parent class? Please find below sample code

public class Fruit {

    public String callingName () {
        //calling specified method getTaste() here 
       // return specified value;
    }
}



public class Orange extends Fruit{
    private String getTaste(){
        return "Orange has sour taste";
    }
}

public class Banana extends Fruit{
    private String getTaste(){
        return "Banana has sweet taste";
    }
}

In this situation, I don't have any reference either to Banana or Orange. The class Fruit itself has to decide which is the right getTaste() will be called from callingName() method. Thanks for any kind help.

Tonny Madsen
  • 12,628
  • 4
  • 31
  • 70
Vierda Mila Nartila
  • 613
  • 1
  • 8
  • 27
  • http://stackoverflow.com/questions/1387207/can-a-parent-call-child-class-methods – Ankit May 09 '13 at 06:49
  • In case you want to create objects of Fruit class, then you cannot make Fruit class as abstract. Do you have any such condition ? – Vineet Singla May 09 '13 at 06:54
  • Thank you for all answers, I really appreciate. Yes I have constraint condition that I cannot know what is exactly object that client pass. It can be Orange or Banana object. I have only to decide from parent class method callingName() which is exact getTaste() method will be calling from object that client pass. Should I using reflections for check the exact class/object type? Thank you – Vierda Mila Nartila May 09 '13 at 08:47

5 Answers5

1

Yes, use an abstract class

public abstract class Fruit {

    protected abstract String getTaste();

    public String callingName () {
         String taste = getTaste(); //calling specified method getTaste() here 
        // return specified value; 
    }
}

You will have to make getTaste() protected in each class.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Try using the concept of factory pattern :

public String callingName (String fruit) {
        switch(fruit){
        case "Orange" :
         return new Orange().getTaste();
        case "Banana" :
            return new Banana().getTaste();
        }
    }

Here you dont need to create Fruit class as abstract and you can create objects of it.

Vineet Singla
  • 1,609
  • 2
  • 20
  • 34
0

Implement

public abstract class Fruit {
    public abstract String getTaste ();
    public String callingName () {
        //calling specified method getTaste() here 
       // return specified value;
    }
}
Tonny Madsen
  • 12,628
  • 4
  • 31
  • 70
0

best way I see, make Fruit abstract since you're not likely to instantiate it.

Give it a protected property called name, and a relevant method (I'd go with getName rather than callingName to follow conventions).

In Orange's and Banana's constructors, just assign the property name with the right value, and override the getName method.

As such:

public abstract class Fruit {
    protected String name;
    public String getName() {
        return name;
    }
}


public class Banana extends Fruit {
    public Banana() {
        name = "Banana";
    }
    public String getName() {
        return super.getName();
    }
}
Mena
  • 47,782
  • 11
  • 87
  • 106
0

It is a built-in feature of Java inheritance. If an instance of extension class, is up-casted, then its method is invoked, its original method will still be invoked. It is also a fundamental concept of object-oriented implementation in Java.

class Fruit {
    protected String callingTaste() {
        return "";
    }

    public String callingName() {
        return callingTaste();
    }
}

You can test above concept using following example:

class FruitProducer {
    public static Fruit[] produce() {
        Fruit[] list = new Fruit[] { new Orange(), new Banana() };
        return list;
    }
}

class UnknownClient {
    public static void main(String args[]) {
        Fruit[] listOfFruit = FruitProducer.produce();
        foreach (Fruit f : listOfFruit) {
            f.callingName(); 
        }
    }
}