Is it possible Constructor Overloading in Java ?

Discussion in 'Object Oriented Concepts' started by vbraojava, Sep 9, 2008.

  1. vbraojava

    vbraojava New Member

    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.
     
  2. shwetamk

    shwetamk New Member

    Is constructor overriding possible in java?

    Is constructor overriding possible in java?

    Thanks.
     
  3. vbraojava

    vbraojava New Member

    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...
     
  4. kmrashish9

    kmrashish9 New Member

    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. pawan_mangal

    pawan_mangal New Member

    Re: Is constructor overriding possible in java?

    wow really good explaination
     
  6. pawan_mangal

    pawan_mangal New Member

    sir i have one problem
    static has some importance in overriding or not
     
  7. thulasiram2085

    thulasiram2085 New Member

    /*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: Oct 3, 2009
  8. thulasiram2085

    thulasiram2085 New Member

    pawan mangal..ur explaination on constructor overriding is really good:)...
     
  9. urpal_priya

    urpal_priya New Member

    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. suneetha211

    suneetha211 New Member

    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. chsrinu

    chsrinu New Member

    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. jagsjava

    jagsjava New Member

    In Java the constructors Overloading is possible but the constructor Overriding is not possible as per the constructor definition
     
  13. anneroberts

    anneroberts Member

    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.
     

Share This Page