简体   繁体   中英

How to call Parent overridden method

I am having two classes Parent and Child.From Child class I am calling parent overridden method (show).From Parent class, I am calling another method(display) but that method is also overridden due to which Child method is called. I want to call the Parent method display from show method.

public class Parent {


    public void show()
    {
        System.out.println("Show of parent ");
        this.display();
    }

    public void display()
    {
        System.out.println("Display of parent");
    }

}

public class Child extends Parent{

    public void show()
    {
        System.out.println("Show of child ");
        super.show();
    }

    public void display()
    {
        System.out.println("Display of child");
    }

    public static void main(String[] args) {
        Parent obj = new Child();
        obj.show();
    }


}

Output :

Show of child 
Show of parent 
Display of child

Need :

Show of child 
Show of parent 
Display of parent

ie I want to call display() method of Parent class from show() method of same class

Whats wrong with:

public void show()
{
    System.out.println("Show of child ");
    super.show();
    super.display();
}

And for the record: you really really really want to put @Override on each and any method that you think overrides something. It happens far to often that you only assume to override something without actually doing it. @Override instructs the compiler to tell you when you make such mistakes.

EDIT: and please note - it seems that you want that show+display are called "together" in certain situations. If so: put only one method on your "interface", not two! What I mean is: if the idea of those methods is to run one after the other, then provide a meaningful way to do that.

In other words: good interfaces make it easy do to the right thing; and hard to do the wrong thing. Having two methods there, and expecting other code to call them in sequence achieves the opposite of that idea. It makes it easy to get things wrong; and harder to get things right!

As finally: already the naming points out that there is a certain design problem at hand. As in: what exactly is the difference between "showing" and "displaying" in the first place?!

public class Parent {


    public void show()
    {
        System.out.println("Show of parent ");
        display();
    }

    public static void display()
    {
        System.out.println("Display of parent");
    }

}

public class Child extends Parent{

    public void show()
    {
        System.out.println("Show of child ");
        super.show();
    }

    public static void display()
    {
        System.out.println("Display of child");
    }

    public static void main(String[] args) 
    {
        Parent obj = new Child();
        obj.show();
    }

}

Explaination:

This has to do with hiding methods. Rules for hiding a method are the same rules for overriding with addtion of static keyword. With static keyword, you are hiding display() method. So when obj calls method show() , computer goes like this: I have object Child with Parent reference(it can be Child reference and output will still be the same in this example). Child object has show() which prints "Show of child" and then calls Parent class's show() method. Parent show() method prints "Show of parent" and then calls display() . Since display() is static , Parent knows only about its own display() method and therefore "Display of parent" is printed.

Although, this is an option maybe it is best not to use this since it can lead to confusing and hard-to-read code.

When you override the display() method in the child, calling it from child object will call the overridden method instead of the parent's because it's overridden. If you want to perform some operation and at the same time call the parent's method as well then you need to call super.display() to execute the parent's display() method. So,

  1. If you want to execute only the parent's display() method then don't override it in the specific child.

  2. If you want to execute only the child's display then existing code is fine.

  3. If you want to call the parent's display() method and also want to perform some additional tasks then mix them with super.display() method.

Example

Based on my above explanation I am making follow 3 child,

Child1 is most probably what you need here as it will print the parent's display() method,

public class Child1 extends Parent{

     // Simply deleting this method will produce exactly the same result
        public void display()
        {
            super.display();
        }

    }

In Child2 we are ignoring parent's display() method and we want the child2's object to perform only the commands written in child2's display() method

public class Child2 extends Parent{

        public void display()
        {
            System.out.println("Display of child");
        }

    }

In Child3 we want both child's and parent's display() method

public class Child3 extends Parent{

        public void display()
        {
             System.out.println("Display of child before parent's display");
             super.display();
             System.out.println("Display of child after parent's display");
        }

    }

Bonus: if you don't want any child to override it's display() method then add a final modifier before the display() method of the parent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM