Tuesday, 3 July 2012

Constructor Overloading



Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists.The compiler differentiates these constructors by taking into account the number of parameters in the list and their type





Save , Compile & Run the Code

Error = ?. Try and debug the error before proceeding to next step.


Every class has a default Constructor. Default Constructor for class Demo is Demo(). In case you do not provide this constructor the compiler creates it for you and initializes the variables to default values. You may choose to override this default constructor and initialize variables to your desired values as shown in Assignment 1.

But if you specify a parametrized constructor like Demo(int a) ,and want to use the default constructor Demo(), it is mandatory for you to specify it.

In other words, in case your Constructor is overridden , and you want to use the default constructor , its need to be specified.

Uncomment line # 4-8. Save , Compile & Run the code

No comments:

Post a Comment