Since you are extending the class A, the main method is also available in class B.So the main method available in class B is called and output hello is displayed.
Static members can be accessed from any subclass as thought the were delared in it. I'm not sure if we can call it inheritance but it works similar. So if in A we has main, B has it as well. But remember static methods can't be overriden, though they can be redefined.
If you are saying that static methods cannot be overridden ...which is right according to me too then how can i write
class A { public static void main(String[] s) { System.out.println("hello "); } } public class B extends A { public static void main(String[] s) { System.out.println("hello b "); } }
This is shadowing not overriding. Below demostrates this. Class A{ public static void staticMethod() { System.out.println("A"); } }
Class B extends A{ public static void staticMethod() { System.out.println("B"); } public static void main(String[] args) { A a = new B(); a.staticMethod(); } }
The output will be "A" not B. If it were overriding the output should have been "B".
joshua i am acceepting the fact that static methods cannot be overridden.....i am just pointing out that i cannot understand the concept which i explained and which you too rightfully reproduced
Hi Vidya Sagar, Actually Amod understands static method cannot be overriden. He is correct in pointing out that it follows rules of overriding . For ex: class A{ public static final staticMethod(){} } class B{ public static staticMethod(){}/*This is not permitted indicating that overriding is taken into account for static method*/ }
Thanks Vidya Sagar, I got it. A method can be declared final to prevent subclasses from overriding or hiding it. It was this bold part that I was looking for.
Vinay, please refrain from duplciate posting. I deleted your duplicate post on this question in this forum. You can always delete your own threads that you create by editing your first post in that forum, and checking the Delete check box.