+ 1

Explanation of this code?

Java Inherintance https://code.sololearn.com/cSKi0U5dFfdf/?ref=app

7th Jun 2022, 8:01 PM
Norberto Costa
Norberto Costa - avatar
6 Respuestas
+ 2
yes if you want to call the parent class's f method, you can use the super keyword: public void f(){ super.f(); System.out.println("b"); }
7th Jun 2022, 8:23 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 2
Thanks. You were kind.
7th Jun 2022, 8:33 PM
Norberto Costa
Norberto Costa - avatar
+ 1
This is an inheritance situation B extends from A therefor B can be handled as A the g method takes an A so B is valid because it extends from A. the output will be 'b' because the class B overrides the definition of the f method. It does becomes a little complex to understand with letters for class and method names so I hope I manage to clarify it a bit.
7th Jun 2022, 8:08 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 1
the method is being overridden so it won't call both the A.f and B.f, only B.f
7th Jun 2022, 8:09 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 1
It's overriden because this methods are not constructors?
7th Jun 2022, 8:17 PM
Norberto Costa
Norberto Costa - avatar
0
So its used a method with an A obj as argument and its called the function. However instead of giving "ab" gives b!
7th Jun 2022, 8:04 PM
Norberto Costa
Norberto Costa - avatar