aspose file tools
The moose likes Beginning Java and the fly likes Java Inheritance : inheriting a main() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Java Inheritance : inheriting a main()" Watch "Java Inheritance : inheriting a main()" New topic
Author

Java Inheritance : inheriting a main()

Abhishekshri Shrivastava
Ranch Hand

Joined: Jan 13, 2011
Posts: 31
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
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2929
    
  15

I would suggest you try by writing simple code. And also dont forget to read a really nice article on Overriding and Hiding


Mohamed Sanaulla | My Blog
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5901
    
    6

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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
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 ?


Joanne
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3795
    
    1

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

Joined: Aug 05, 2005
Posts: 3011
    
    9
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

Joined: Jan 03, 2004
Posts: 5901
    
    6

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.

 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Java Inheritance : inheriting a main()
 
Similar Threads
ststic variables
WHy does main() work with private accessibility?
Overridden methods
It is Strange about main method
Can a main method be inherited?