*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How main in superclass is called from subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How main in superclass is called from subclass" Watch "How main in superclass is called from subclass" New topic
Author

How main in superclass is called from subclass

Vinay Yepuri
Greenhorn

Joined: Jun 11, 2006
Posts: 8
class A
{
public static void main(String[] s)
{
System.out.println("hello ");
}
}
public class B extends A
{
}

Here i saved this file as B.java and compiled. when i run this B.java it is calling the main method from the super class A. how it is possible
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
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.
Lukas Stephienn
Ranch Hand

Joined: Dec 23, 2005
Posts: 60
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.


SCJP 5.0 (91%), SCBCD 1.3 (94%), SCWCD 1.4 (88%)
Amod Mulay
Ranch Hand

Joined: Apr 06, 2006
Posts: 33
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 ");
}
}

though i am overriding
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
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".

Please notify me if I am wrong.
Regards,
Joshua
Amod Mulay
Ranch Hand

Joined: Apr 06, 2006
Posts: 33
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
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Hi Amod Mulay

Execute the below code and see



Now you tell what you understand from this
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
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*/
}
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Check this

overriding and Hiding
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Easier one


JavaRanch
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
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.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Amod Mulay
Ranch Hand

Joined: Apr 06, 2006
Posts: 33
hey thanx i am a little bit clear about it....though not fully convinced
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How main in superclass is called from subclass