• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Constructor overloading

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
--------------------------------------------------------------------------------
class ThreeConst {
public static void main(String[] args) {
new ThreeConst();
}

public void ThreeConst(int x) {
System.out.print(" " + (x * 2));
}

public void ThreeConst(long x) {
System.out.print(" " + x);
}

public void ThreeConst() {
System.out.print("no-arg ");
}
}

The above code prints no output but my doubt is as per overloading rule different arg. type only allowed then how is it possible?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the three are methods they are not constructors because they have a return type. In this class a default constructor will be added by the compiler.

-Jerry
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prabakar,

My understanding is this not overloading the constructor but it is new method with the same name as constructor. As you have mensioned the return type it will not be taken as constructor signature it will be considered as a method signature only....

Regards,
Vinoth M
 
prabakaran perumal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vinoth

It means that we can create method with same constructor name. Am i right?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prabhakar,

Constructor's name is nothing but the Class's name. And constructor do not contain any return type. (if it is containing then it's method).
You can have method with the class/costructor name. No problem.
But the method is not invoked when you create instance of class, if you want to invoke that method you have to invoke it explicitely.

...yogesh
 
prabakaran perumal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks... I got it.
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic