How to access a non static method in an abstract class's static method?

how to access overridden, non static method of abstract class in subclass

  • In my program there are only two classes one is Parent ,which is an abstract class and having a non static concrete method, void show(), now there is another class, Child which extends the abstract class, Parent and override the show() method. So now is there any way to access the method of abstract class from main method of Child class with out calling another non static method of Child class.

  • Answer:

    This is what you're looking for: super.show(); although that'll only work from within a (non-static) member function of the subclass. There's no direct way to call an overridden method from another class. Your only option would be to have the subclass expose a new method that does nothing but call the parent's overridden method. However if you do need to do that it suggests that your class hierarchy is incorrectly designed...

user571616 at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Since Child.show() is static, you must do this: class Child extends Parent { static void show() { Child c = new Child(); c.myShow(); } void show() { super.show(); } } Answer to the edited post: you cannot call a non-static method without an instance. Every other detail of this situation is irrelevant if you can't/don't want to create an instance of either Parent or Child, as it is just no possible. The question now becomes: Why would you want to do that? What are trying to achieve?

Viruzzo

To access parent methods you prefix the call with "super". So, in your case use: super.show();

Roger Lindsjö

A child class can always use the super keyword to access a method of its superclass. So, in Child class, you may do super.show();.

DejanLekic

Use super.show(): class Parent { public void show() { System.out.println("Parent.show"); } }; class Child extends Parent { @Override public void show() { super.show(); } };

hmjd

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.