[Solved] Putting method in “return”, in Java, is the correct approach?


You seem to be asking about whether the above code is the correct way to write a method that overrides a method in its superclass.

Yes it is the correct way to do it.

Yes, that is a good way to “change behavior” … though you are only changing the behavior for the new class (B)

What if returned objects with the ability to modify?

Same answer.

As it is with returning of static and nonstatic methods?

No. Static methods are non-polymorphic. You cannot override static methods.

In your example, if bB was static then the @Overrides annotation would give you a compilation error. If you removed it, you would find that the call to bB() in aA would always call A.bB and never B.bB.

Note that calling a static method with the syntax this.someStaticMethod(...) is legal, but bad style. It looks like the call will be polymorphic but in fact it isn’t.

Whether this has any implications for concurrent programming?

No particular implications.


On the other hand, if you are actually asking about methods that return other methods … your example code doesn’t do that.

8

solved Putting method in “return”, in Java, is the correct approach?