To understand the result it is necessary to dismember the calculation below in four parts.
int r = obj1.metodoX() + obj2.metodoX() + obj2.metodoX(5) +
obj3.metodoX(10, 100);
Part 1: obj1.metodoX()
The obj1 corresponds to the class Classea. The execution of metodoX() returns the value 10.
Part 2: obj2.metodoX()
The obj2 corresponds to Classeb. Despite the obj2 inherit the methods of Classea, the execution of metodoX() is that of Classeb, so that the returned value is 100.
Part 3: obj2.metodoX(5)
This call executes the metodoX(int n) of Classeb, which in turn returns the result of the expression metodoX() + n
. The metodoX() called in that expression is that of Classeb, that returns the value 100, that is, the result of this execution is 100 * 5
that is 500.
4th part: obj3.metodoX(10, 100)
In the above call another method of the Classeb calling to the methods of Classeb, returning the value 1100.
So the end result is 10 + 100 + 500 + 1100 = 1710.
On the issue of inheritance, note that there are methods of Classea with the same name in Classeb.
In this example found by you, the Classeb inherits the methods of Classea, but the methods of Classea were not used in the classeB. For this reason, all calls on Classeb refer to the methods themselves and not to those of the parent class.
So that, in the classeB a call to a method "metodoX()" of Classea, the call should be preceded by the pointer super.
A brief example would be:
public classe ClasseB extends ClasseA {
public int metodoX(){
return 100
}
public int metodoX(int n){
return super.metodoX() * n
}
public int metodoX(int m, int n){
return super.metodoX(m) + metodoX()
}
}
With the above example, the inheritance would be effectively used in the code, so that the final result would be different.
Regarding the overload, it was used when the classes were implemented with methods of the same name, but with different signatures.
Could you explain where the confusion went!
– novic
Did you forget to put in most of the semicolons or was it the people who did the contest that made them?
– Victor Stafusa
@Victorstafusa I was like this, I just copied the paste.
– Denis Caixeta
Hahaha, so the correct answer would be "compiler error" and you could file an appeal to dismiss the matter. If you consider that missing semicolons would be added, then you would give 1710 and the answers below already explain why.
– Victor Stafusa
@Virgilionovic The instantiation "Classea obj2 = new Classeb()", because they did so?
– Denis Caixeta
@Deniscaixeta Remember that
ClasseB
inherits fromClasseA
, then a variable of typeClasseA
can store an object of the typeClasseB
. The methods that can be called are those of the variable type (ClasseA
), but the methods that are actually called are those of the object in it (ClasseB
). That is to say,obj2.metodoX(1, 2)
is a build error, butobj2.metodoX()
calls the methodmetodoX()
ofClasseB
.– Victor Stafusa
Okay, I get it. Thank you.
– Denis Caixeta
@Deniscaixeta in this specific case
ClasseA
in the instance of the variableobj2
is the guy, but, its instance is ofClasseB
which inherited the behaviour ofClasseA
, although in this the expected behavior is theClasseB
because he rewrote the methodsmetodoX()
andmetodoX(int n)
. The method contained in Classeb:metodoX(int m, int n)
is not part of theClasseA
, so it doesn’t even show up. It’s a good question many who work with POO doesn’t know it, and it was really cool to post your doubt, congratulations.– novic