A constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object It can be used to initialize the objects ,to required ,or default values at the time of object creation It is not mandatory for the coder to write a constructor for the class |
If no user defined constructor is provided for a class, compiler initializes member variables to its default values.
- numeric data types are set to 0
- char data types are set to null character(‘\0’)
- reference variables are set to null
In order to create a Constructor observe the following rules
1) It has the same name as the class
2) It should not return a value not even void
Assignment 1: Create your First Constructor
Step 1: Type following code in your editor
Step 2) Save , Run & Compile the code. Observe the output.
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
Examples of valid constructors for class Account are
Assignment 2:To understand Constructor Overloading
Step 1) Type the code in editor
Step 2) Save , Compile & Run the Code.
Step 3) Error = ?. Try and debug the error before proceeding to next step.
Step 4) 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.
Step 4) Uncomment line # 4-8. Save , Compile & Run the code.
constructor chaining
Consider a scenario where a base class is extended by a child .
Whenever an object of the child class is created , the constructor of the parent class is invoked first.
This is called Constructor chaining.
Assignment 3: To understand constructor chaining
Step1 ) Copy the following code in the editor
Step 2) Run the
Code. Owing to constructor chaining , when object of child class
DemoChild is created , constructor Demo() of the parent class is invoked
first and later constructor DemoChild() of the child is created.
Expected Output =
Inside 1st Parent Constructor
Inside the Constructor of Child
Value1 === 1
Value2 === 2
Value1 === 3
Value2 === 4
Step3 ) You may
observe the constructor of the parent class Demo is overridden . What is
you want to call the overridden constructor Demo(int a) instead of the
default constructor Demo() when your child object is created ???
In such cases you can use the keyword "super" to call overridden constructors of the parent class.
Syntax:-
Ex: If your constructor is like Demo(String Name,int a)
you will specify super("Java",5)
If used ,the keyword super needs to be the first line of code in the constructor of the child class.
Step 4) Uncomment Line # 26 and run the code. Observe the Output
No comments:
Post a Comment