aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overloading of main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overloading of main()" Watch "overloading of main()" New topic
Author

overloading of main()

amit mandal
Ranch Hand

Joined: Aug 13, 2010
Posts: 46


when i am trying to overload the main method in the class Ex1, am getting compiler error "Ex1.java:10: cannot declare both main(java.lang.String[]) and main(java.lang.String...) in Ex1
public static void main(String[] aa)"

but when i am keeping main(String[] aa) in the child class X, the code is running absolutely fine! please explain.

ocpjp 6.0 certified.
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
As I know there is no such thing as static overriding or overloadinbg, because static is bound to classes and not to any specific object instance, saying so, your first methods are hiding each other and your third method in the different class is not related to them.


love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

A class cant have two methods with the same signature.
The var-args method that you have used:
is internally treated as:As this and the one you specified:
both are having the same signature, thats why the compiler is complaining.
However in the second case you are basically redefining the static method of class Ex1 in its subclass x, so I don't think there is any problem in that.The point to remember here is that : The execution will begin from the main() method of that class, whose name is same as that of your .java file.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

Imad Aydarooos wrote:As I know there is no such thing as static overriding or overloadinbg

there cannot be overried methods but there can be overloaded static methods


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
Prasad Kharkar wrote:
Imad Aydarooos wrote:As I know there is no such thing as static overriding or overloadinbg

there cannot be overried methods but there can be overloaded static methods


You are right Prasad, I could say:
we can overload static methods in the same class, but we can not overload them in the sub class, because they are not inherted. if we try to overload static method in the sub class we are only creating a new method not related to the suber class method.
thanks Prasad
amit mandal
Ranch Hand

Joined: Aug 13, 2010
Posts: 46

Imad Aydarooos wrote:
Prasad Kharkar wrote:
Imad Aydarooos wrote:As I know there is no such thing as static overriding or overloadinbg

there cannot be overried methods but there can be overloaded static methods


You are right Prasad, I could say:
we can overload static methods in the same class, but we can not overload them in the sub class, because they are not inherted. if we try to overload static method in the sub class we are only creating a new method not related to the suber class method.
thanks Prasad

overloading is nothing but name reusing. except the name the overloaded method might have no commonality with the overloading method. only the compulsory condition is that the signature of the methods should be different. Yes the static methods cant be over-ridden as there exit only 1copy of the static method. even though we can define same method in the child class but the child class method act as a "shadow" and hides the parent class method.
anyway...if we talk about the signature shouldnt the String[] aa be different from String... aa??
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
Sorry Amit mandal and the others who read this thread if I confused you, because I'm totaly wrong in my phrases quoted below this corrections:
1- static members are sure inherted by the subclasses.
2- while every instance of the superclass or subclass gets its own copy of the instance members, there is only one copy of the static memeber and its bind to the class who declare it, so the instances of the super class and the subclases only refer to the super class static member but they dont have thier own copy of it.
3- because of all above: a subclass can override his own copy of the instance method, but he can't override his superclass static method(because he dosn't own a copy of it, there is only the superclass copy). only he can hide it with a method with the same name and signature and return type.(in this case you can refer to the superclass metod as:SuperClassName.method() and to the subclass method with the same signature as: SubClassName.method().
4- Sure you can overload a static method in a superclass with one in the subclass as you can see from the below code:


if you compile and run the code you will get
C:\myprogs>java Inner
Amethd()
Amethod()verlding
Imad Aydarooos wrote:
Prasad Kharkar wrote:
Imad Aydarooos wrote:As I know there is no such thing as static overriding or overloadinbg

there cannot be overried methods but there can be overloaded static methods


You are right Prasad, I could say:
we can overload static methods in the same class, but we can not overload them in the sub class, because they are not inherted. if we try to overload static method in the sub class we are only creating a new method not related to the suber class method.
thanks Prasad
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

I guess you got that right Imad.

but he can't override... only he can hide it


You have given your class a gender too

anyway...if we talk about the signature shouldnt the String[] aa be different from String... aa??


Nope. A String... is converted to a String[] internally. That is what allows a var arg to function the way it does.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
You have get it Deepak, my classes has TYPE, Behavior and (GENDER too )

Thanks alot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overloading of main()