Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:24:12+00:00 2026-06-14T11:24:12+00:00

Is it possible in Java to invoke an overridable method in such a way

  • 0

Is it possible in Java to invoke an overridable method in such a way that it always executes the “locally defined” version rather than an overridden version from a subclass? I.e. is there an analog to super that refers to this class, rather than the super class?

Let me give a code example to hopefully make it clear what I’m trying to do:

class A {
    void foo() {
        System.out.println("Foo from A");
    }

    void bar() {
        foo();  // <-- This is the important line!
    }
}

class B extends A {
    @Override
    void foo() {
        System.out.println("Foo from B");
    }
}

If I do new B().bar(), it will call the bar() method defined in A, which calls foo() as overridden in B to print “Foo from B”.

Is there a way that I can force the bar() method to call the foo() method as defined in A rather than B? Just like I can use super.foo() in B to call the foo() method as defined in A? Unfortunately using this.foo() still calls the version of the subclass. Even something like ((A) this).foo() or A.this.foo() doesn’t work.

Clearly, I could simply define a private or final version of foo() in A and call that instead. But I am hoping for a solution, where all I do is change the “important line” in the code sample above to a different way of invoking foo() to have it print “Foo from A”, preferably without some trick like reflection.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:24:13+00:00Added an answer on June 14, 2026 at 11:24 am

    Either make your methods static (baadddddd), either change your design.

    Indeed, it makes no sense to provide the default behavior for a subclass that it is defined to adapt itself to the concerned method.

    As your foo() method seems to vary, you may implement a Strategy Pattern like this:

    interface BarProcess{
        void foo();
    }
    
    public class DefaultBarProcess implements BarProcess{
        void foo() {
           System.out.println("Foo from A");
        }
    }
    
    public class AnotherBarProcess implements BarProcess{
        void foo() {
           System.out.println("Foo from B");
        }
    }
    
    class A {
    
        private BarProcess barProcess;
    
        public A(Bar barProcess){
          this.barProcess = barProcess;
        }
    
        void bar() {
            barProcess.foo();
        }
    }
    
    class B extends A {  //deprecated! No need to exist
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do I invoke a private static method using reflection (Java)? So
In both Java and C# it is possible to invoke a private method via
Possible Duplicate: Java: Always override equals? should I override equals function for any class
Possible Duplicate: Java: how to check that a string is parsable to a double?
is it possible to invoke .jnlp file from java code? Example: Process p =
Is it possible to invoke Java EE web services from iPhone? and how?
I am trying to create some MySQL code that will invoke a Java program
Is it possible to invoke Java RMI asynchronously? I'd like my RMI call to
Is it possible to invoke a method where property is expected in JSF 2.0
Is it possible to execute a statement such as m.invoke(param1 ,param2) in a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.