let us first define what is a constructor
Constructor: -
A constructor is something that has the name same as that of the class name and it is used to construct the values those are created by objects
Whenever an object is created in a class then the constructor of that respective class is called.
for example, the constructor in a class can be as follows
class FirstClass // name of the class
{
FirstClass() // this is a constructor
{
// the coding of constructor goes here
} // end of constructor
}
In the example above the constructor is with no arguments. There can be constructors with no arguments or constructor with arguments.
There are some rules that need to be followed while declaring and defining a constructor
1.The constructor should have the name of the class itself (Always remember java is a case sensitive language)
2.The constructor cannot have a return type
3.The access specifier of the constructor is the access specifier of the respective class if not given.
4.Constructor does not return any value
The valid declaration for the constructor is shown above
Now let us see what makes the constructor call with some programming examples
class Constructor
{
Constructor() // no argument constructor
{
System.out.println("First Constructor");
}
Constructor(int a,int b) // two argument constructor
{
System.out.println("Second Constructor");
}
Constructor(int a,int b,int c) // three argument constructor
{
System.out.println("Third Constructor");
}
public static void main(String args[]) // Main Started
{
Constructor a = new Constructor(); // this call no-arg constructor
Constructor b = new Constructor(10,20); // this calls two arg constructor
Constructor c = new Constructor(10,20,30); // this calls three arg constructor
}// End of Main
}// End of class
Now look at the code above whenever the objects are created respective constructors are called and the output is as follows
Quote
F:\Java\Concepts\DeclarationAndAccessControl>java -cp F:\Java\Concepts\DeclarationAndAccessControl Constructor
First Constructor
Second Constructor
Third Constructor
First Constructor
Second Constructor
Third Constructor
Another thing about the constructor is that if the derived class constructor is called using the object of derived class then the constructor of the base class is also called.
This is because the JVM calls the constructors of the parent classes using the super reference to the parent class
the complete procedure will be shown in another tutorial which will explain the use or super and this reference.
to elaborate this more
consider the following code
this is how we write the code in the text editor
class SomeClass
{
}
and here is what the compiler takes it as
class SomeClass
{
SomeClass()
{
super();
}
}
Now wait here
even if we have not declared a constructor here then how come the constructor was defined automatically, the fact is that every class has a default constructor despite we declare it or not. it has a call to the parent class with super
following example shows it thoroughly
class BaseClass
{
BaseClass()
{
System.out.println("This is base class constructor");
} // end of base class constructor
BaseClass(int x)
{
System.out.println("This is another base class constructor");
} // end of another constructor
}
class DerivedClass extends BaseClass
{
DerivedClass()
{
System.out.println("This is derived class constructor");
}
public static void main(String args [])
{
DerivedClass dc = new DerivedClass(); // this calls the base class constructor also
}
}
The output is
Quote
This is base class constructor
This is derived class constructor
This is derived class constructor
Hope this explains much about the constructor







MultiQuote







|