• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

overriding of static method

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Arvind.

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

Sampath
 
Ranch Hand
Posts: 982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic