Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling base class overridden function from base class method

public class A {     public void f1(String str) {         System.out.println("A.f1(String)");         this.f1(1, str);     }      public void f1(int i, String str) {         System.out.println("A.f1(int, String)");     } }    public class B extends A {     @Override     public void f1(String str) {         System.out.println("B.f1(String)");         super.f1(str);     }      @Override     public void f1(int i, String str) {         System.out.println("B.f1(int, String)");         super.f1(i, str);     } }   public class Main {     public static void main(String[] args) {         B b = new B();         b.f1("Hello");     } } 

I'm seeking that this code would output:

B.f1(String) A.f1(String) A.f1(int, String) 

Yet I'm getting:

B.f1(String) A.f1(String) B.f1(int, String) A.f1(int, String) 

I understand that under the context of B "this" in A.f1(String) is B's instance. Do I have the option to do the chain new B1().f1(String) -> (A's) f1(String) -> (A's) f1(int, String) ?

This is a theoretical question, practically the solution would obviously be in A to implement a private function that both f1(String) and f1(int, String) would call.

Thank you,
Maxim.

like image 932
Maxim Veksler Avatar asked Jan 29 '11 11:01

Maxim Veksler


People also ask

How do you call a base class overridden method?

Invoking overridden method from sub-class : We can call parent class method in overriding method using super keyword. Overriding and constructor : We can not override constructor as parent and child class can never have constructor with same name(Constructor name must always be same as Class name).

How do you access the overridden method of base class from the derived?

6. How to access the overridden method of base class from the derived class? Explanation: Scope resolution operator :: can be used to access the base class method even if overridden. To access those, first base class name should be written followed by the scope resolution operator and then the method name.

How can we access the base class implementation of an overridden method from derived class object Java?

Answer is using super keyword.

How can we access base class implementation of an override method?

An override method is a new implementation of a member that is inherited from a base class. The overridden base method must be virtual, abstract, or override. Here the base class is inherited in the derived class and the method gfg() which has the same signature in both the classes, is overridden.


2 Answers

Unfortunately, no

As i'm sure you're aware, but I'll state explicitly for completeness - there are only the 2 keywords to control the method invocation:

  • this - this.method() - looks for method starting from the invoking instance's class (the instance's "top" virtual table - implied default)
  • super - super.method() - looks for method starting from the parent class of the class in which the invoking method is defined (the invoking class' parent's virtual table - not strictly true, but simpler to think of this way - thanks @maaartinus)

I can imagine another keyword (e.g. current?) do what you describe:

  • current - current.method() - looks for method starting from the class in which the invoking method is defined

but Java doesn't have such a keyword (yet?).

like image 162
Bert F Avatar answered Oct 14 '22 20:10

Bert F


I'm afraid, it's impossible, but there's a simple workaround:

public class A {     public void f1(String str) {         System.out.println("A.f1(String)");         privateF1(1, str);     }      private void privateF1(int i, String str) {         System.out.println("A.f1(int, String)");     }      public void f1(int i, String str) {         privateF1(i, str);     } } 
like image 33
maaartinus Avatar answered Oct 14 '22 21:10

maaartinus