-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NepaliGuy wrote:
1. Can subclass inherit the static methods?
2. Can you overide the static menthods?
Hi,
Both of these are impossible. Ignoring reflection, the only way to
call a static method is if you already know the classname at compile
time. If you already know the classname, it makes no sense to inherit
a static method, since it would never get called anyway. I'm aware
that you can call static methods through an instance of a class, but
this is considered bad. Having just run a quick test, I realize why:
the static method called is selected by the declared type of the
variable, not the actual type of the object! For example, given
classes A and B, where B extends A, and where both define a static
method x(), the following code:
A a = new B();
a.x();
actually ends up calling A.x(), since the runtime type of a is
ignored.
The fact is, there is no practical use for inheriting or overriding
static methods, since there is no way for a subclass's version to be
called polymorphically anyway. You can define a new static method of
the same name in a class that just happens to be a subclass, though I
assume this doesn't count as overriding.
If you do happen to be using reflection, and need a class (whose name
is to be determined at runtime) to have a certain static method, your
only option is to write this requirement in documentation and hope
the client obeys it.
- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQE/KmiqwxczzJRavJYRAl8qAJ9wZeNP2Tgiz/Vt23eODIrycwiLgACdFv0O
LSlj90XqpaIohDjKtkOj+A4=
=ofLX
-----END PGP SIGNATURE-----