• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Static methods.

 
sriannapoorna Balasubramanian
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between overriding and redefining of static methods.
it is confusing.Please explain me.
 
Praveen Babu
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 643
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 643
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant detailed explanations gowher amin naik.

I thought you deserved some credit


David
 
sriannapoorna Balasubramanian
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Everyone for the effort.i am now clear in this concept.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic