Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overloading of main()

 
amit mandal
Ranch Hand
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have get it Deepak, my classes has TYPE, Behavior and (GENDER too )

Thanks alot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic