Java Techies - Solution for All
Java Techies - Solution for All
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 there is no user defined constructor provided for a class, compiler initializes member variables to its default values.
In order to create a Constructor there are some rules to follow
class Example
{
int value1;
int value2;
Example()
{
value1 = 10;
value2 = 20;
System.out.println("Inside Constructor");
}
public void display()
{
System.out.println("Value1 === "+value1);
System.out.println("Value2 === "+value2);
}
public static void main(String args[])
{
Example d1 = new Example();
d1.display();
}
}
Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists. Constructor overloading in java allows to have more than one constructor inside one Class.
Constructor overloading is not complex you just need to create another constructor, obviously same name as of class but different signature but there are certain rules related to Constructor overloading which needs to be remembered while overloading constructor in Java.
Overloading Rules :
class Example
{
int value1;
int value2;
Example()
{
value1 = 10;
value2 = 20;
System.out.println("First Constructor");
}
Example(int a)
{
value1 = a;
System.out.println("Second Constructor");
}
Example(int a,int b)
{
value1 = a;
value2 = b;
System.out.println("Third Constructor");
}
public void display()
{
System.out.println("Value1 "+value1);
System.out.println("Value2 "+value2);
}
public static void main(String args[])
{
Example d1 = new Example();
Example d2 = new Example(30);
Example d3 = new Example(30,40);
d1.display();
d2.display();
d3.display();
}
}
If we consider a scenario in which a base class is extended by a child class. Whenever an object of the child class is created , the constructor of the parent class is invoked first.This is called Constructor chaining.
class Example
{
int value1;
int value2;
Example()
{
value1 = 1;
value2 = 2;
System.out.println("First Parent Constructor");
}
Example(int a)
{
value1 = a;
System.out.println("Second Parent Constructor");
}
public void display()
{
System.out.println("Value1 "+value1);
System.out.println("Value2 "+value2);
}
public static void main(String args[])
{
ExampleChild d1 = new ExampleChild();
d1.display();
}
}
class ExampleChild extends Example{
int value3;
int value4;
ExampleChild()
{
//super(1);
value3 = 3;
value4 = 4;
System.out.println("Child Constructor");
}
public void display()
{
System.out.println("Value1 "+value1);
System.out.println("Value2 "+value2);
System.out.println("Value1 "+value3);
System.out.println("Value2 "+value4);
}
}
Use of This For constructor .
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 0, 0);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
...
}
class Loan{
private double interest;
private String type;
public Loan(){
this("personal loan"