Java
Constructor
A constructor is invoked to create an object
and prepares it to be used. A constructor looks
like a method without a return type and is named
with the exact name of the class. A constructor
may or may not accept arguments. Let us look at
the example provided below.
package com.example.core.oop4;
public class Parent {
/*
* A constructor must have no return parameter.
* A constructor can have any access modifier.
* A constructor must have exactly the same name and
* the same Upper and Lower case as compared
* to the Class name.
* A constructor that accepts no parameter is the
* default when no other constructor is declared.
* In this case, we declared a constructor, the
* default constructor is no longer there.
*/
public Parent() {
System.out.println("Parent no argument constructor.");
}
/*
* A Class can have multiple constructors.
* However, it should have different signatures.
*/
Parent(String param) {
System.out.println("Parent with String argument"
+ " constructor." + param);
}
/*
* The signature of constructors depends on the number
* of parameters, the order of the
* parameters and the type of the parameters.
*/
Parent(String param1, int param2) {
System.out.println("Parent with String and int "
+ "parameters constructor." + param1 + " "
+ param2);
}
Parent(int param1, String param2) {
System.out.println("Parent with int and String "
+ "parameters constructor." + param1 + " "
+ param2);
}
}
package com.example.core.oop4;
/**
* A constructor is used to instantiate an object.
* This is to demonstrate the concept and rules of
* constructors.
*
* @author Rolan Liwanag
*
*/
public class Child extends Parent {
public Child(String param) {
System.out.println("Child constructor with String "
+ "parameter. " + param);
}
public Child(String param1, int param2) {
super(param1);
System.out.println("Child constructor with String "
+ "and int parameter. " + param1 + " " + param2);
//super(param1, param2);
/*
* The call to Parent(String param1, int param2)
* will cause compilation problem since call to
* super can only happen in the first line of code
* inside the constructor.
*/
}
public Child(String param1, int param2, int param3) {
this(param1);
/*
* this(param1,param2) is a call to
* Child(String param1, int param2) and can only
* happen in the first line of code inside the
* constructor.
*/
System.out.println("Child constructor with String, "
+ "int and int arguments. " + param1 + " "
+ param2 + " " + param3);
}
public static void main(String[] args) {
/*
* Child class dont have the default no argument
* constructor nor a declared no argument constructor
* is declared. Since there is another constructor
* declared, the default constructor is no longer
* available.
*/
//Child childObj1 = new Child();
Child childObj1 = new Child("abc");
/*
* As you may have noticed in the output, when we
* made a call to the Child(String param)
* constructor, it made a call before anything else
* to the Parent() constructor.
*/
Child childObj2 = new Child("abc", 1);
/*
* As you may have noticed in the output as well in
* the code, the Child(String param1, int param2)
* constructor has a super(param); call. This means
* invoking the Parent(String param) constructor.
* Constructors can only be invoked within another
* constructor and call to super() can only happen
* in the first line of code inside the invoking
* constructor. It also happens by default just like
* what happened in the instantiation of childObj1.
*/
Child childObj3 = new Child("abc", 1, 2);
/*
* In this case, when Child(String param1, int
* param2, int param3) constructor was invoked,
* it made a call to Child(String param) explicitely.
* When the previously mentioned constructor was
* executed, it made a default call to super() then
* continued executing the rest of the code inside
* the Child(String param) constructor.
*/
}
}
Back Next