• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please clarify my doubts about static methods

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,
I have read several posts about static method confusion which says "static method are not inherited and also they can't be overridden "

but still I have doubt

Here is the coding for my question



As in the comment statement Uncommenting the first method(which has Integer as return type )and commenting the second method makes compilation success but in reverse I got this error.



override ???

Since static methods are not inherited
1. why the compiler let us do that ?
2. It also accepts the subtype of superclass static method return method (which is one of the rule for overriding -in my case Integer is-a Number )

So,what I understood is, even though static methods are not inherited and can't be overridden we must adhere to overriding rules while using static methods .
Am I correct or misunderstood ?

Please clarify me .

Thanks in advance
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanks,
Mohammad
[ October 17, 2008: Message edited by: Mohammad Khan ]
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class G1
{
static void method1()
{
System.out.println("method in super class");
}
}
class G2 extends G1
{
static void method1()
{
System.out.println("method in sub class");
}
}
public class CheckStatic
{
public static void main(String[] q)
{
G1 w=new G2();----(1) //G2 w=new G2();---(2)
w.method1();
}
}


complation is succeed for the above code.

case (1):
if you are using superclass reference variable which is used to call method1 which shows the output as "method in super class".

case (2):
if you are using sub class reference variable then you will get ouput as "method in sub class".

so method execution is depends upon the reference variable which is used to call the method1.

but in case of overriding if you mention line ---(1)
then we should get output as "method in sub class".

so,here static methods are not overriding.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

when we create a static method inside a sub class with the same heading as in a super class, it won't be consider as the overrided method. It will treat as a separate method.

And the call to the static methodwill depends on the refernce only. not on the object, which is used for calling the method. So, eventhough the supar class refernce hold a subclass object and calling a static method, the superclass method will be called.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Parthiban,
as per your question I am trying to answer that when you try to override a static method ,it's seems that it is following overriden rules.But when the the time comes you try to use it then it checks the refference type not object type. So actually rules for overriden are same only this(object type).And the method are not actually overriden.
 
Parthiban Malayandi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Thanks for your replies.
 
reply
    Bookmark Topic Watch Topic
  • New Topic