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 overriding of static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding of static method" Watch "overriding of static method" New topic
Author

overriding of static method

Naresh Saw
Ranch Hand

Joined: Sep 03, 2005
Posts: 66
hi all

here is my code.

class Super

public void m1(){
System.out.println("super m1");
}

public static void m2(){
System.out.println("super static m2");
}

}

public class Child {
public void m1(){
System.out.println("child m1");
}

public static void m2(){
System.out.println("child static m2");
}

public static void main(String arg[]){
Child t = new Child();
System.out.println((Super)t.m1()); // 1
System.out.println((Super)t.m2()); // 1
}
}


and the output is :

child m1
super static m2


why ?

anybody can explain overriding of static method.


naresh<br />SCJP 1.4(86%), SCWCD 1.4(78%)
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
A static method cannot be overriden it can only be hided. It doesnt involve in polymorphic behaviours since it belongs to a class and not to an instance.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
hi srini,
ya static methods cannot be overridden.
i.e if we have static methoids in the super as well as the child class always the super class static method is executed,even if we call thru an instance of the child class .And they call this hiding ...
how ya??can u explain..
my question may be silly ,but if possible tel me wats hiding here


Harish<br />SCJP 1.4 (85%)
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
how ya??can u explain..

Harish Please take care to post in proper english , it's good to avoid SMS language like 'u' , 'r' etc

Now comming to your question, The static method in the subclass shadows the static method in the superclass. Static are figured out compile time and instance method are figured out at run time.
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
Is it true that static method can override another override method only through inheritance?

or can static method overload another static method
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
if we have static methoids in the super as well as the child class always the super class static method is executed,even if we call thru an instance of the child class .

Harish,
First of all the code mentioned above wont compile as you are returning void from the method & using it in System.out.println(..)
See the static method in the super class wont execute always , Call the static method without type casting it to Sure Class.
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
Static are figured out compile time and instance method are figured out at run time.
thats wat canu explain this with an example
sorry shall not use SMS language frm now
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
thats wat canu explain this with an example
sorry shall not use SMS language frm now

Thats great but again you used it there


As i said in my previous reply ( sorry for the typo there ) Try to call the Static method withou type casting it to Super Class like t.m2() and now try to cast it and call the Static method.
Arvind Giri
Ranch Hand

Joined: Jun 26, 2005
Posts: 91
Hi Harish,

Here is an example



Output :

static method1 of super class
static method1 of sub class
method2 of sub class
method2 of sub class


Hope this helps you


Regards<br /> <br />Arvind Giri<br />MCA,SCJP 1.4,SCWCD 1.4<br />Looking for SCDJWS
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
The code given by in Naresh Saw beggining is not complining in JDK1.4
Got the error message as
---------- Javac ----------
test1.java:28: inconvertible types
found : void
required: Super
System.out.println((Super)t.m1()); // 1
^
test1.java:29: inconvertible types
found : void
required: Super
System.out.println((Super)t.m2()); // 1
^
2 errors
-------------------------------------------------------

Please explain me clearly about this .

ALso tell me whether static method can be overloaded or not
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
m1() and m2() are declared as void. That is they have no return type. So the cast to Super cannot be applied.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
Thank You Arvind.

given a good piece of code to understand the static overridden methods.

Sampath
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 978
Hi,

Regarding karthiks post,

Please explain me clearly about this .

ALso tell me whether static method can be overloaded or not




Yes you can overload static methods just like any methods..

Consider...methods in Math class..

Math.abs(float f)
Math.abs(long l)
Math.abs(int i)

Regards
[ September 28, 2005: Message edited by: A Kumar ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding of static method
 
Similar Threads
programfollow
Overriding Vs Shadowing
Hiding or overriding static methods
calling super class methods
testing String as immutable