Overriding static methods : Whats wrong with these classes??

A

ankur

package Pack1;

public class SuperA {


public int supervarA;
public void superMethA()
{

System.out.println("Package Pack 1 Class SuperA");
}

public static void methstaticA()

{
System.out.println("In class SuperA");
}
}


package Pack1;

package Pack1;

public class SubD extends SuperA{


public static void main(String args[])

{
SubD varD1 = new SubD();
varD1.methstaticA();
}
public static void methstaticA()

{
System.out.println("In class SubD");
}


}


SubD prints : In class SubD
 
M

Mike Schilling

The title of your post expresses a confusion: it's not possible to
override a static method, but merely to hide one. In the fragment:

Type1 var1 = new SubType1();
var1.staticMethod1();

It's the declared type of the reference var1 (i.e. Type1) that's used
to find the version of staticMethod1() to call, not the actual type of
the object it references (i.e. SubType1). It's IMHO unfortunate that
Java allows static methods to be called using references; it would be
clearer and no less expressive to require that they be called via a
type name, e.g.

Type1 staticMethod1();
 
A

ankur

Side note: Java naming conventions call for package names to comprise all
lower-case characters.  (In real life, package names that include variations
on "package" within their names do not make good names.)



Java naming conventions call for word parts within names to be capitalized,
such as "superVarA".  (In real life, "super" and "var" would be name parts to
avoid in a variable.)




Likewise this should be named "methStaticA" by convention (except that in real
life "meth" and "static" would be name parts to avoid).


Further side note: do not use TAB characters to indent Usenet posts, use
spaces.  Two (or up to four if you like) spaces are enough per indent level.
Usenet readers often have more restrictive width constraints than your
favorite source editor (although many folks recommend keeping source lines
limited in width, too).  TABs tend to render posted code less readable,
hindering efforts to help you with your question.

Sorry to all for posting a vague question, not using the naming
convention and using tabs in usenet posts. I will avoid this going
forward. Coming to the question, all I was saying is that I was able
to override static method methstaticA() in SubD because it printed "In
class SubD" when called main(). If I comment out the definition for
methstaticA() in SubD then I would get "In class SuperA". In a way
didn't I override the static method ?? But I understand what Mike
Schilling's is saying in his post that polymorphic behavior cannot be
implemented by overriding static methods!
 
A

ankur

No, you didn't.  You hid the static method.  If you had overridden it, then a
call via the superclass would get the subclass behavior.  That will not
happend.  As Mike Schilling pointed out:




If "staticMethod1" were overridden the behavior would be different, because
you would get the subclass behavior.  But static methods cannot be overridden,
so you get the superclass behavior.

Read the Java Language Specification (JLS) on the difference between
overriding, overloading, hiding, obscuring and shadowing.

<http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.3.1>
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.3>
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8> ff.
(
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8....>
is especially interesting.)

<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8....>

and elsewhere.

BTW, the JLS touches on naming conventions in
<http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.8>

Thanks to all !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top