File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static method overriding

 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 232
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, all! Especially Jane. Your example was lucid and illustrated the point well and clearly.
Shashi
 
sarim raza
Ranch Hand
Posts: 232
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic