Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding 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 "Regarding Static methods." Watch "Regarding Static methods." New topic
Author

Regarding Static methods.

sriannapoorna Balasubramanian
Greenhorn

Joined: Aug 24, 2006
Posts: 9
Hi,

Static methods cannot be overriden right!!!Please read the following program.

Question:
class TestSuperClass{
static String s1="Super S1";
String s2="Super S2";
static void printS1(){
System.out.println(s1);
}
void printS2(){
System.out.println(s2);
}
void printBoth(){
printS1();
printS2();
}
}

class TestSubClass extends TestSuperClass{
static String s1="Sub S1";
String s2="Sub S2";
static void printS1(){
System.out.println(s1);
}
void printS2(){
System.out.println(s2);
}
public static void main(String args[]){
new TestSubClass().printBoth();
}
}

Answer is :Super S1 Sub S2.

How is it possible?whether static methods can be inherited?
Thanks in Advance.
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
Hi,

Static methods are not overidden but can be hidden. This means a method with same method signature of its base class can be written in derived class. However, polymorphism doesnt work for such methods.

Regards,
Praveen
Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
Method overriding and method redefining are two separate concepts. Here you are using the facility of method redefining. To check overriding, assign subclass object to superclass reference and then overridding would come into the picture.

Write code and see the result.
sriannapoorna Balasubramanian
Greenhorn

Joined: Aug 24, 2006
Posts: 9
what is the difference between overriding and redefining of static methods.
it is confusing.Please explain me.
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
In overiding, when we assign a subclass object to a base class reference, at runtime, the subclass method is called.

While in redefining, it calls the base class method.

To see this, Try to change your code to make a subclass object assigned to the base class reference like,

BaseClass obj = new SubClass();
obj.methodName();

and see the output for both static and non-static methods.
[ August 31, 2006: Message edited by: praveen babu ]
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

Check code above it shows that statics methods cannot be overloaded but static base method can be redefined in child class.Redefining and overloading is totaly different.
sriannapoorna Balasubramanian
Greenhorn

Joined: Aug 24, 2006
Posts: 9
First of all,Sorry for raising 3 threads.Accidentally there were 2 threads raised unknowingly with same subject.i am new to this group so i accidentally pressed 2 times the Post New Topic Button.

i can now understand the difference between overriding and redefing in general,but regarding the following example,

In the main method,there is a line "new TestSubClass().printBoth(); "
this line has no reference type.May i know in that case,how it behaves and it takes the output as Super S1 Sub s2?it should have taken sub s1 sub s2

Question:
class TestSuperClass{
static String s1="Super S1";
String s2="Super S2";
static void printS1(){
System.out.println(s1);
}
void printS2(){
System.out.println(s2);
}
void printBoth(){
printS1();
printS2();
}
}

class TestSubClass extends TestSuperClass{
static String s1="Sub S1";
String s2="Sub S2";
static void printS1(){
System.out.println(s1);
}
void printS2(){
System.out.println(s2);
}
public static void main(String args[]){
new TestSubClass().printBoth();
}
}
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

when new TestSubClass().printBoth()is executed
the control goes to printBoth() method at line 10.
At line 11 printS1() is executed and control goes to line 4
because printS1() is static.There is static method printS1()
in class TestSubClass also.But that will not be executed
because static methods are choosen as by class name.
After printS1() in class TestSuperClass will finish execution
control will go printS2() at line 7 but printS2() is
overridden at line 22 and is not static so printS2()
at line 22 will be executed.
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Originally posted by sriannapoorna Balasubramanian:
First of all,Sorry for raising 3 threads.Accidentally there were 2 threads raised unknowingly with same subject.i am new to this group so i accidentally pressed 2 times the Post New Topic Button.

Whether you're new to this forum or not has little to do with pressing a button twice...
static method overloading/overriding is a so much of a standard question that perpetually confuses newbies. I'll check to see if it's included in the FAQ somewhere and if not, I'll get to it sometime (hopefully).
Sashi
David Kennedy
Ranch Hand

Joined: Jan 22, 2006
Posts: 33
Brilliant detailed explanations gowher amin naik.

I thought you deserved some credit


David


"There are only 10 types of people in the world: Those who understand binary, and those who don't"
sriannapoorna Balasubramanian
Greenhorn

Joined: Aug 24, 2006
Posts: 9
Thanks Everyone for the effort.i am now clear in this concept.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Static methods.