+ 1

override problem

in this code how can I access the parent version of the method g() https://code.sololearn.com/chEnp6TsyN7N/?ref=app

21st Jul 2020, 5:23 PM
ABADA S
ABADA S - avatar
5 Réponses
+ 1
I think, You cannot call overridden method by sub class object. Only overridden method will be binded to that object. If you want to call super class method, then call it from subclass method by super keyword. Or else call by super class instance. Like new A().g(); in f() method. Or super.g(); from sub class method.
21st Jul 2020, 6:55 PM
Jayakrishna 🇮🇳
+ 1
If you want to access g from within the implementation of class B, call super.g(). It is more difficult to call A's version of g() in b from Program class. You'd need to implement a method in A or B that deligates to A's version of g. There is some discussion on a related topic at: https://coderanch.com/t/410314/java/call-class-superclass-method-class
21st Jul 2020, 5:50 PM
Josh Greig
Josh Greig - avatar
+ 1
Josh Greig I want to keep f in A call the parent version of g even if it called from B object
21st Jul 2020, 5:52 PM
ABADA S
ABADA S - avatar
0
thank you master Jayakrishna🇮🇳
21st Jul 2020, 7:07 PM
ABADA S
ABADA S - avatar
0
public class Main { public static void main(String[] args) { int a = 5; double b = 10.2; System.out.println(doubleTheValue(a)); System.out.println(doubleTheValue(b)); } //complete the method for integer value type public static int doubleTheValue(int a) { return a*2; } //overload the method for double value type public static double doubleTheValue(double a) { return a*2; } }
27th Sep 2022, 4:09 PM
Parthasarathi Panda
Parthasarathi Panda - avatar