0 Java knows that a method is a constructor because it has the same name as the class itself and no return value.
1 Constructors can not be inherited
2 When declaring constructors for your class, you can use the normal access specifiers to specify what other objects can create
instances of your class.
3 Either this or super can be called as the first line from within a constructor, but not both (or compiling error). Note that This is true if this or super is used to invoke a constructor; if you use this or super to invoke a method, you can place them anywhere
anytimes.
class Base{
public Base(){}
public Base(int i){}
}
public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver(10);
}
MyOver(int i){
super(i);
}
MyOver(String s, int i){
this(i);
}
}
4 Constructors are called from the base (ancestor) of the hierarchy downwards.
class Mammal{
Mammal(){
System.out.println("Creating Mammal");
}
}
public class Human extends Mammal{
public static void main(String argv[]){
Human h = new Human();
}
Human(){
System.out.println("Creating Human");
}
}
The output of above is:
"Creating Mammal"
"Creating Human"