0

I have a method in abstract class that may be overriden in extended class or not. I'd like to call the original (not overriden) method. How to reference it? Example:

public abstract class A{

  protected MyResult my_method(){
    MyResult myResult;
    ...
    ... // Default implementation
    ...
    return myResult;
  }

 ...

 private void xy(){
   // I'd like to call my_method here
   if(!my_method().test()){
     // The function is not implemented well, I want ot use the original (abstract) method
     ...
     ... log a message for programmer
     ...
     this::A.my_method(); // I need something like this
   }
 }
}

I don't need any advice how to do it by different way. I only ask if there is a java syntax for referencing methods or properties in original class or in distant super-class.

0

1 Answer 1

2

I'd like to call the original (not overriden) method.

You can't do that from outside the subclass. From the subclass itself, you can call

super.my_method();

which will always call the superclass implementation, even if it's been overridden in this class. But to be able to do that outside the subclass would reduce encapsulation.

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

3 Comments

Your suggestion is usable in class B which extends A. I need to call it from a method written in A class.
@Hink: You can't do that. If you want to call code in class A from class A, put it in a final method to avoid the subclass being able to override it.
Ok, I belive You, I must reconcile with it.

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.