• Post Reply Bookmark Topic Watch Topic
  • New Topic

Diffence between constructor overloading and method overloading  RSS feed

 
Slaxmi Raj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between constructor overloading and method overloading in java?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question isn't specific to the Servlet API and would sit better in our Beginning Java forum.
I will move it there for you, but please take care to pick a suitable forum related to your question.
Also, you should use a meaniful topic title.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swarajya. Welcome to The Ranch!

Other than one refers to constructors and the other to methods, there's not a great difference. You can have multiple constructors, or you can have multiple methods with the same name, as long as the types of the arguments are different.

There's one important point that's only relevant to constructors. Calling a constructor always has to result in the superclass constructor being eventually called. So normally you either put an explicit call to super(...) on the first line, or the compiler adds an implicit super() constructor call at the beginning. However, if you have overloaded constructors, you have have one constructor call another instead. For example:


Does that answer your question, or did you have something more specific in mind?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is worth learning about the construct this(...); whereby you can call other constructors in the same class. You may do well to use this(...); in each constructor, so all constructor calls converge on a single pathway to one constructor, which calls the superclass constructor.
You also need to know when you have to write super(...); and when you can allow the javac tool to add it implicitly.
 
Sccot Smith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructor'name is same to class'name,But method is not!That's the difference!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!