Can inner class extends outer class, or opposite?

Q. Can inner class extends outer class, or opposite? Why we should generally avoid using this technique?
A: Yes, it is legal (inner class extends outer class, or opposite).
However, you would be better off by not using these features.
An advice from SUN's training class: Don't use inner class unless it is a very simple case.
Actually, abuse of inner class is much worse than the abuse of old goto. This is my opinion, but not only mine. A lot of companies have very strict coding standard to eliminate abuse of inner classes too.
You are better-off without using inner classes in complicated projects.
Inner class only gives you some convenience in simple cases, such as Adapter etc.
Just consider the following scenario:
We make total 10 levels of nested inner classes.
1) Let the 10th level inner class extends 5th level inner class which in turn extends outer class.
2) Let 7th level inner class extends 4th level inner class which in turn extends 2nd level of inner class.
3) The outer class extends 2nd level inner class (legal).
4) The 3rd level has 4 different inner classes, which extends different inner or outer class individually.
5) One of them has a method3(), which has a local class extends outer class.
6) ...
The complexity can go on, and on, and on, and o...
Unpredictable results are guaranteed.
Got the picture? Remember, compiler is written by human beings, and uses certain grammar rules to do the job. The grammar rules only can handle limited complexity. The set of combination and permutation of things are unlimited.
Grammar rules are not Newton's Law, or Einstein's Law of Relativity, which are not made by human, but we discovered them.
Assumption of programmer will not do certain things (such as the above crazy stuff, even it is legal) will be made for cost, efficiency, and simplicity purposes. If you really want to, you can drive compiler crazy!
In real life programming, if you find something does not work as you expected, try alternatives, or report a possible bug (or a undocumented feature ).
And it will never be perfect, and it need not to be one either...
Write good enough software.
One more point: Why I was/am so passionate to express my view to against abuse of inner classes? Because I'm a big believer of KISS. "Keep It Simple and Straight forward" or "Keep It Simple and Stupid". I'm sure a lot of visitors of this site are/will be leading software engineers/project leaders/manages/etc in
the software industry. If some of you will think twice when you start to write code like some inner extends outer or opposite, it will be "music" to my ears.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章