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
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
Related Q & A:
- How to mock static method call by constructor?Best solution by Stack Overflow
- What is the difference between a static method and class method in Python?Best solution by pythoncentral.io
- How to get actual return type of a generic static method in Java?Best solution by stackoverflow.com
- How do I add to an array from a static method?Best solution by stackoverflow.com
- How can we get SBJSON connectionDidFinishLoading method inside array data in another class?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.