aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please clarify my doubts about static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please clarify my doubts about static methods" Watch "Please clarify my doubts about static methods" New topic
Author

Please clarify my doubts about static methods

Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
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
Mohammad Khan
Ranch Hand

Joined: Sep 23, 2008
Posts: 37


Thanks,
Mohammad
[ October 17, 2008: Message edited by: Mohammad Khan ]
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
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.


SCJP5 and SCWCD1.5
Think Twice Act Wise...
Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
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.


Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
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.


Thanks,<br />Subhasish
Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
Hi all,
Thanks for your replies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please clarify my doubts about static methods