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

overriding of static method

 
Naresh Saw
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 481
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1228
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1228
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Karu Raj
Ranch Hand
Posts: 481
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m1() and m2() are declared as void. That is they have no return type. So the cast to Super cannot be applied.
 
sampath garapati
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Arvind.

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

Sampath
 
A Kumar
Ranch Hand
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic