• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Inheritance : inheriting a main()  RSS feed

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can somebody help me to understand that is "public static void main(String[] args)" be inherited or not??
surely we can inherit a static method. main() is also a static method. so it should be inherited. How does JMV works for main() execution.

Thanks
 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you try by writing simple code. And also dont forget to read a really nice article on Overriding and Hiding
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Abhishekshri Shrivastava wrote:Hi,

Can somebody help me to understand that is "public static void main(String[] args)" be inherited or not??
surely we can inherit a static method.



Well, it's not "surely". That is, there's no reason to think that it's a given, natural law of OO or something. However, in Java's case, yes, the Java Language Spec provides that static methods can be inherited.

How does JMV works for main() execution.



That's a very broad, vague question. You'll need to be more specific.

If you're wondering if a class that doesn't define its own method, but inherits one from a superclass, can be invoked as the main class to the JVM, then, as the other respondent suggested, just try it and see. Either it works or it doesn't, and then you have your answer either way.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:However, in Java's case, yes, the Java Language Spec provides that static methods can be inherited.


Can you provide a link to the section in the JLS that says that ?
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joanne Neal wrote:Can you provide a link to the section in the JLS that says that ?


It partly depends on what you mean by inheritance of a static method. But this will compile:
I'd count that as inheritance....but it can't be overridden.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Matthew Brown wrote:I'd count that as inheritance....but it can't be overridden.


You're right (sorry Jeff). Monday morning, pre-caffeine is my excuse.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Matthew Brown wrote:

Joanne Neal wrote:Can you provide a link to the section in the JLS that says that ?


It partly depends on what you mean by inheritance of a static method.



Actually, I'd say it depends on what the JLS means by inheritance. In case anybody is still interested, it's 6.4.3 The Members Of A Class Type. It doesn't explicitly mention static vs. non-static, so both can be inherited.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!