Hi, As per Khalid Mughal 's book and JLS ,you can use final modifier with main() .It has also been discussed in this forum http://www.javaranch.com/ubb/Forum24/HTML/001021.html However,even after racking my brains alot ,I still cannot think of a situation where it can be used .Could somebody tell me of a situation where we can use it as such? Thanks Ira ------------------
The reason for making a method final is so that subclasses can not override that method. So the reason to make main() final would be for that reason. However, in a real application, you would generally only have one main method that kicks off your app, and I guess you could make this final so that no one can extend that class and override, but I have never used it before. I have put main methods in other classes for debugging purposes, but those mehtods I do want to override, so I wouldn't make those final. My only thought is that if you have one class whose main purpose is to kick off your app with all the initial values, you may want to make it final, so no one could override it. But like I said, I wouldn't really use this because if that class was just to kick off the app, that is not really a class I would be subclassing.
hi guys, don't think static method can't be overriden, it can be hidden by implementation in subclass instead. either, i don't get the point why one would bother to write the entry of an application twice. but one thing for sure is that final doesn't appear in the recommended form of main() as an entry of an application anywhere. so, just forget it. regards rong
Joined: Jun 30, 2000
I have heard people before saying overridden and hidden. What is the difference between the two? When you override a method, you have a subclass that has a method that is the same method name, with the same types and number of arguments. Thus you are overriding the method with the method of the subclass. There are other rules, like you can't make the method of the subclass more restrictive, can't make it non-static if it is static in the superclass, and you can't throw any checked exceptions that were not thrown by the superclass unless the checked exception is a subclass of the checked exception that was thrown in the superclass. But what do you mean by hidden? I keep thinking they are the same, you have overloading and overriding and the later is the same as hiding. Can anyone tell me the difference and if I am missing something? Thanks, Bill
Alright, then from the message in the link it looks like: Overriding is when you call Parent p = new Child(); and then when you call a method for p you will get the method that is in Child. But Hiding is when you call Parent p = new Child(); and then when you call a method for p you will get the method that is in Parent. And the only way this happens is when you have a private method in Parent and then have the same method in Child but declared as public. Still slightly confused, but I think I am getting it.
Hiding actually means you "shadow" the visibility of the method in the superclass. It has more to do with name-space overlaps than subclassing or inheritance. You can observe runtime binding( aka late binding )polymorphism wherever overriding is allowed. However, with static methods, all the method calls are resolved and bound to the references at compile time( aka early binding). IMO this is the difference between overriding and hiding. HTH Ajith
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Overriding and hiding are different. 1. Non-static methods are resolved at runtime, based on the real type of object. So a subclass non-static method with the same name and signature as a superclass non-static method overrides the superclass method. 2. Variables are resolved at compile-time, based on the type of the reference. So a subclass variable with the same name as a superclass variable hides the superclass variable. 3. Static methods are resolved and compile-time, based on the type of the reference. They don't participate in dynamic binding. So a subclass static method with the same name and signature as a superclass static method hides the superclass method. Hope this helps.