Is it possible Constructor Overloading in Java ?

#1
Yes, Constructor Overloading is possible in Java.

Constructor Overloading : Writing two or more Constructors in a Class with the same name but with a difference in the parameters is called Constructor Overloading.

- If we don't write any Constructor in a Class, Java Compiler will write a Default Constructor and initialize the instance variables with the default values, if we do not initialize the instance variables.
 
#3
Re: Is constructor overriding possible in java?

Is constructor overriding possible in java? Thanks.
Hi Shweta,

It is never possible.

Constructor Overriding is never possible in Java.

See below explonation then you can know why it is not possible :

Constructor means its look like a method but name should be as class name and no return value right.

Now come to ur question.... Overriding means the same thing what we have declared in Super class that exactly we have to declare in Sub class it is called Overriding.

Now see... Super class name and Sub class names are different.

If you trying to write Super class Constructor in Sub class then Sub class will treat that as a method not constructor why because name should not match with Sub class name. And it will give an compilation error that methos does not have return value. So we should declare as void, then only it will compile.

See below example you can have a better idea :
Code:
Class One
{
-----
one() // [COLOR="Magenta"]Super Class constructor[/COLOR]
{----}

one(int a) // [COLOR="magenta"]Super Class Constructor Overloading[/COLOR]
{-------}

}

Class Two extends One
{
one() // [COLOR="red"]this is a method not constructor because name should not match with Class name[/COLOR]
{}

two() // [COLOR="Magenta"]sub class constructor[/COLOR]
{}

two(int b) // [COLOR="magenta"]sub class constructor overloading[/COLOR]
{}
}
If you have any queries on this let me know...
 
#4
sir,i m bit confused that is constructor overloading ppossible in java or not,since oncce u have answered that it is possile bou again u have written it is not possible
 
#5
Re: Is constructor overriding possible in java?

Hi Shweta,

It is never possible.

Constructor Overriding is never possible in Java.

See below explonation then you can know why it is not possible :

Constructor means its look like a method but name should be as class name and no return value right.

Now come to ur question.... Overriding means the same thing what we have declared in Super class that exactly we have to declare in Sub class it is called Overriding.

Now see... Super class name and Sub class names are different.

If you trying to write Super class Constructor in Sub class then Sub class will treat that as a method not constructor why because name should not match with Sub class name. And it will give an compilation error that methos does not have return value. So we should declare as void, then only it will compile.

See below example you can have a better idea :
Code:
Class One
{
-----
one() // [COLOR="Magenta"]Super Class constructor[/COLOR]
{----}

one(int a) // [COLOR="magenta"]Super Class Constructor Overloading[/COLOR]
{-------}

}

Class Two extends One
{
one() // [COLOR="red"]this is a method not constructor because name should not match with Class name[/COLOR]
{}

two() // [COLOR="Magenta"]sub class constructor[/COLOR]
{}

two(int b) // [COLOR="magenta"]sub class constructor overloading[/COLOR]
{}
}
If you have any queries on this let me know...
wow really good explaination
 
#7
/*ill try to explain about constructor overloading----
def:Writing two or more constructors in the same class,but with the difference in its parameters is called "contructor overloading".
i hope my explaination will giv u better idea..
if u want,u can copy n run this pgm*/

Code:
class Person
{
 String name;
 int age;

 Person() //default constructor
 {
 name="thulasi";
 age=24;
 }
     public void talk()
     {
      System.out.println("Name="+name);
      System.out.println("Age="+age);    
      }
 Person(String s,int i) //parameterised constructor
 {
 name=s;
 age=i;
 }
}

class Demo
{
 public static void main(String ram[])
 {
 Person p1=new Person();  //object using its default constructor
    p1.talk();
 Person p2=new Person("swathi",23); //same object using its parameterised constructor
    p2.talk();
 }
}
 
Last edited:
#9
Hi VbRao Sir,

I'm new member. I freqently come across your answers and they are really very useful. When ever I check a question I try to find out for your reply. Your explanation about overriding is really very informative. Thank you so much for your contribution.

Priya.
 
#10
Hi Priya,

Have alook over the above example.

He is always saying Constructor Overloading is Possible

COnstructor overriding is not possible.

Is this fine?

Have a glance over above example
 
#11
yes ,constructor overloading is possible in JAVA,itaht means Different Number of parameters to constructors ,type of parameters are different then automatically that constructor is called
 
#12
Hi Priya,

Have alook over the above example.

He is always saying Constructor Overloading is Possible

COnstructor overriding is not possible.

Is this fine?

Have a glance over above example
In Java the constructors Overloading is possible but the constructor Overriding is not possible as per the constructor definition
 
#13
Recently in on account for strategy overloading you have numerous strategies for those same name Anyhow separate signature, in constructor overloading you have various constructors with an alternate mark for those main distinction that constructor doesn't need a come back sort to java.
 
Top