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.
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.
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
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.
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