Overloading Constructors : Constructor « Object Oriented « SCJP






class Base {
   public Base(String s) {
   }
   public Base(int i) {
   }
 }

 class Derived extends Base {
   public Derived(String s) {
     super(s);
   }
   public Derived(int i) {
     super(i);
   }
 }








6.7.Constructor
6.7.1.Constructor
6.7.2.super() and this()
6.7.3.Constructors have no return type and their names must exactly match the class name.
6.7.4.Constructors are used to initialize instance variable state, as follows:
6.7.5.Constructors can use any access modifier, including private.
6.7.6.How compiler generates the constructor
6.7.7.What happens if the super constructor has arguments?
6.7.8.Overloaded Constructors
6.7.9.A constructor is invoked with a call of the form new YourClassName(arg1, arg2 ,... ).
6.7.10.The constructor is called a no-arguments constructor if the argument list is empty.
6.7.11.If you do not explicitly code any constructors, the compiler automatically creates a default constructor
6.7.12.You can pass control to a constructor in the parent class by using the keyword super.
6.7.13.To control the particular constructor that is used, you simply provide the appropriate arguments.
6.7.14.Overloading Constructors
6.7.15.Use this to call overloaded constructor
6.7.16.Java insists that the object is initialized from the top of the class hierarchy downward
6.7.17.Compiler puts in a call to the no-argument constructor in the parent
6.7.18.If you extend a class without no-argument constructor, then you must explicitly call super().
6.7.19.Constructors Throwing Exceptions
6.7.20.Invoking constructors in the parent class must be in the first line of code in the constructor body.
6.7.21.A class can have multiple constructors, but they must have different parameter lists.
6.7.22.A constructor can invoke another constructor, this must be done in the first line of code in the constructor body.
6.7.23.The only way to abort a constructor is to throw an exception.