File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes The game of statics Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "The game of statics" Watch "The game of statics" New topic
Author

The game of statics

Harshad Raut
Greenhorn

Joined: Sep 23, 2005
Posts: 1
I read on all the forums so many times that Static methods cannot be inherited and cannot be overridden. But I came across the code which is allowing me to override and inherit static methods. Please go through the code below for overriding the static method.


package foo;

class A{

A(){
System.out.println("Here is A's constructor");
}

public static void print()
{
System.out.println("Printing A");
}

}

public class B extends A{

B(){
System.out.println("Here is B's constructor");
}

public static void print()
{
System.out.println("Printing B");
}


public static void main(String[] args){

B b = new B();

B.print();

A.print();

}
}

The output I got for this was allowing me to override this method print. It worked fine. Please let me know if it is really possible to override and inherit static method.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Harshad,

Originally posted by Harshad:I read on all the forums so many times that Static methods cannot be inherited and cannot be overridden. But I came across the code which is allowing me to override and inherit static methods. Please go through the code below for overriding the static method.


Whar do you mean by inheritance ?

try this :
A a = new B();
a.print();
you will know it by yourself !


Thanks & Regards,<br />T.Srinivasan,<br />SCWCD 1.4(89%),SCJP 5.0(75%)<br />"That service is the noblest which is rendered for its own sake." - Mahatma Gandhi
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
That example doesn't demonstrate overriding.

Static methods can be inherited by subclasses.

However, you cannot override them.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Keith,

I just want Harsad to know the fact that "overriding will not take place by that code."

You got me wrong. So sad.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
Sorry Srinivasan. My post was a comment to the OP, not a criticism of your post.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32654
    
    4
Harshaut Raut, welcome to the Ranch.

Posted by Srinivasan thoyyeti
try this :
A a = new B();
a.print();
Now try thisYou have two separate methods in different classes, which just happen to have the same name.

Try this:-Result: won't work. You are not overriding at all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The game of statics
 
Similar Threads
abstract
Contructor "good or bad"
Question on overriding
Polymorphic behavior provided by inheritance programming assignment
Static Methods in an Interface