File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static method overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static method overriding" Watch "Static method overriding" New topic
Author

Static method overriding

Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
If B is a subclass of A and A has a static method, will the method remain static in B? If B overrides this method, should it remain static?
Thanks,
Sashi
Santosh Jaiswal
Greenhorn

Joined: Oct 04, 2000
Posts: 26
You can't override static methods in subclasses. But if you define a method in subclass with same signature as in Super class with static keyword, you may access the Super class method in sub class as classname.methodname() as you in following code
class A {
static void method1() {
System.out.println("Super");
}
}
public class B extends A {
public static void main (String arg[]) {
A.method1();
method1();
}
static void method1() {
System.out.println("Subclass");
}
}
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Santosh, it looks like you are overriding the method1() of the parent class but your reply categorically states that you can't override a static method in a child class. Can you please clarify?
Thanks,
Sashi
parag bharambe
Ranch Hand

Joined: Sep 01, 2000
Posts: 41
You can override the static method in the subclass, compiler will not show an error. But by Clasname.methodName you will access the superclass version only not of subclass.
Check it.
parag bharambe
Ranch Hand

Joined: Sep 01, 2000
Posts: 41
You can override the static method in the subclass, compiler will not show an error. But by Clasname.methodName you will access the superclass version only not of subclass.
Check it.
sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
A static method cannot be overriden to be made non static and vice versa.
I am not aware if overriding is allowed or not, from Santosh's code it seems that it is ok, as long as the overriding method is also termed as static.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Saskihanth,
Santosh is right. Static methods cannot be overridden; however, they can be hidden and this is what you are seeing in the example code.
Normally, the JVM uses the actual runtime type of an object vs the declared type when a method is invoked. Because the rules state that a static method cannot be overridden; if the method being invoked is 'static', the compiler simply codes for the declared type; it does not defer the choice until runtime.
I've modified Santosh's example to add a non-static overridden method.
<pre>
class A {
static void method1() {
System.out.println("Super");
}

void method2() {
System.out.println("Super method2");
}
}
public class B extends A {
public static void main (String arg[]) {

A a1 = new B();
a1.method1(); // uses method1() in A
a1.method2(); // uses method2() in B

B b1 = new B();
b1.method1(); // uses method1() in B
((A)b1).method1(); // uses method1() in A
}
static void method1() {
// hides method1() in A
System.out.println("Subclass");
}

void method2() {
// overrides method2() in A
System.out.println("Subclass method2");
}
}
</pre>
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiousity.
-- Dorothy Parker
[This message has been edited by Jane Griscti (edited November 05, 2000).]
[This message has been edited by Jane Griscti (edited November 05, 2000).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Thanks, all! Especially Jane. Your example was lucid and illustrated the point well and clearly.
Shashi
sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
i wanted to know what are the rules governing this declaration:
where A is superclass and B is subclass.
A a = new B
what are the rules of access for the reference variable a, in relation to variables and methods in class A and class B
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static method overriding