1

Calling the fruitName method inside Fruit constructor, is actually delegating the call to the child Apple class's method!

public class CallingParentMethodInInheritanceHierarchy {

abstract class Fruit {
    String fruitName;

    public Fruit(String fruitName) {
        this.fruitName = fruitName;

        /*
         * o/p - Inside constructor - Child: Fruit name is - Apple
         */
        System.out.println("Inside constructor - " + fruitName()); // doubt?
    }

    public String fruitName() {
        return "Parent: Fruit name is - " + fruitName;
    }

    public abstract String type();

}

class Apple extends Fruit {
    public Apple() {
        super("Apple");
    }

    public String fruitName() {
        /*
         * To call the super class method, only way is -
         * 
         * System.out.println(super.fruitName());
         */
        return "Child: Fruit name is - " + fruitName;
    }

    @Override
    public String type() {
        return "AllSeasonsFruit";
    }
}

public static void main(String[] args) {
    Fruit fruit = new CallingParentMethodInInheritanceHierarchy().new Apple();

    /*
     * o/p - Child: Fruit name is - Apple
     */
    System.out.println(fruit.fruitName());
}

}

The main attempt behind this, is that I was trying to call the parent method without using the trivial way super.fruitName() call inside a child method.

Please help me @line #12

4
  • It's not trivial; it's the only way. There's no difference in the behavior between parent and child - you're printing out the name. Why override it? Commented Nov 21, 2015 at 15:54
  • 1
    methods called in constructors should be final. Commented Nov 21, 2015 at 15:55
  • @duffymo - I am just checking, later I can add real functionality :-) Commented Nov 21, 2015 at 15:57
  • Sorry, you either understand polymorphism or you don't. Commented Nov 21, 2015 at 16:01

2 Answers 2

4

This is polymorphism 101. The most specific - i.e. lowest on the inheritance tree - version of a method is invoked within a class hierarchy. If you had not overridden the fruitName() method at all then the base class method would be called.

Sign up to request clarification or add additional context in comments.

6 Comments

But I am calling inside the parent constructor, at this time the child object would not be completely instantiated. Please help if I am wrong.
That is a different question - and it would not work the way you are trying. You are correct that the child would not yet be available. But that does not change that invoking the method would attempt to invoke the child. So you need to rethink your hierarchy.
@53by97 Consider this: you can already call abstract methods of Fruit in its constructor, and Apple's code will thus be invoked. Similarly, Apple's code will run in place of the overridden method.
@53by97 If you want to invent your own programming paradigm then have at it. We are just relating how existing OO - as implemented in Java - works.
@53by97 If it still didn't click for you, you may try researching how OOP is implemented in Java on the lowest level. Particularly, you might want to learn about virtual method tables.
|
3

Because that's how polymorphism works. Child classes can override any method that is not final, and parent code won't be able to tell the difference. That's why non-abstract non-final public methods are generally discouraged.

7 Comments

Why do you say that non-abstract non final public methods are discouraged? That's a foundation of OO design: to provide implementation but allow specialization when needed.
@javadba Because this violates the Open-closed principle. Implementing abstract methods instead of overriding non-abstract methods can also be used to achieve specialization, and in a much less error-prone way.
Consider the case that a default publicly accessible behavior (i.e. method) were desired yet it should be permitted to override that behavior in subclasses. Please illustrate how that would be achieved.
@javadba By having that default behavior outside the parent class. Dude should not have any default behavior, instead there should be some DefaultDude that provides the behavior by implementing an abstract method of Dude. If you need to modify the DefaultDude, use the Decorator pattern.
OK I see. I'll buy that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.