• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How main in superclass is called from subclass

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this

overriding and Hiding
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Easier one


JavaRanch
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thanx i am a little bit clear about it....though not fully convinced
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic