+ 1
override problem
in this code how can I access the parent version of the method g() https://code.sololearn.com/chEnp6TsyN7N/?ref=app
5 ответов
+ 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.
+ 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
+ 1
Josh Greig
I want to keep f in A call the parent version of g even if it called from B object
0
thank you master Jayakrishna🇮🇳
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;
}
}