Vierda Mila Nartila
Vierda Mila Nartila

Reputation: 613

How to calling specified method from parent class?

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.

Upvotes: 0

Views: 160

Answers (5)

user2118735
user2118735

Reputation:

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(); 
        }
    }
}

Upvotes: 0

Vineet Singla
Vineet Singla

Reputation: 1677

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.

Upvotes: 1

Mena
Mena

Reputation: 48404

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();
    }
}

Upvotes: 0

Tonny Madsen
Tonny Madsen

Reputation: 12718

Implement

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

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533442

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.

Upvotes: 1

Related Questions