• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How main in superclass is called from subclass

 
Vinay Yepuri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amod Mulay
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amod Mulay

Execute the below code and see



Now you tell what you understand from this
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this

overriding and Hiding
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Easier one


JavaRanch
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amod Mulay
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanx i am a little bit clear about it....though not fully convinced
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic