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.
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).
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.
Answer is using super keyword.
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.
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.method()
- looks for method starting from the invoking instance's class (the instance's "top" virtual table - implied default)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.method()
- looks for method starting from the class in which the invoking method is definedbut Java doesn't have such a keyword (yet?).
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With