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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值