Monday, February 9, 2009

Constructors

The purpose of Constructor is to perform of our creted object. Whenever we are calling new operator for the creation of object, it calls constructor automatically to provide initialization for the object.
class Student
{
String name; int rno;
Student(String name, int rno) ----> Constructor
{
this.name=name;
this.rno=rno;
}
public static void main(String a[])
{
Student s=new Student(“xxx”,101);
Student s1=new Student(“yyy”,102);
-------
------
}
}
Rules Of Constructor :
1. Constructor concept is applicable for every class including abstract class also.
2. Interface doesn’t have Constructor’s concept.
3. The name of the const4ructor and the name of the class must be same.
4. The allowed modifiers for the constructors are public, private, protected, and default. If you are applying any other we will get a CTE saying “modifier xxx not allowed her”.
5. We can’t give return type for the constructor even void also.

If we will give return type for the constructor that thing as a method instead of constructor that thing as a method instead of constructor (so,there is no CTE). Ie., it is legal (but stupid) to have a method whose name same as classname.
Default Constructor:-
If the programmer is not writing any constructor, then only compiler will generate a default constructor.
Ie., either programmer written constructor or compiler generated must present in your class but not both at a time.
Prototype of default constructor shown below:
a).Programmer written code:- class Test{}
Compiler generated code:-
class Test
{
Test()
{
super();
}
}
  • The default constructor is always no argument constructor.
  • The access modifier of the default constructor is sane as access modifier of the class (public & default only).
  • The default constructor contains only one statement which is ‘no arg call to super class Constructor ‘ (super();)
b) Programmer written code:-
class Test {
Test(int i)
{
System.out.println(“constructor”);
} }
Compiler generated code:-
class Test {
Test(int i)
{
super();
System.out.println(“constructor”);
} }

c). Programmer written code:-
class Test {
Test(int i)
{
super();
System.out.println(“Hai”);
} }
Compiler generated code:- no new code is going to generate.

d). Programmer written code:-
class Test {
void Test()
{
System.out.println(“hello”);
}
}
Compiler generated code:-
class Test {
void Test()
{
System.out.println(“hello”);
}
Test()
{
super();
} }
e). Programmer written code:-
class Test {
Test()
{
this(10);
System.out.println(“hai”);
}
Test(int i)
{
System.out.println(i);
} }
Compiler generated code:-
class Test {
Test() {
this(10);
System.out.println(“hai”);
}
Test(int i) {
super();
System.out.println(i);
} }
  • The first line inside a constructor must be a call to super class constructor ( by using super();) or a call to overload constructor of the same class. (by using ‘this’ keyword).
  • If you are not writing the first line as either ‘super()’ or ‘this’ then compiler will always keep a no arg call to super class constructor (super();).
1. Allowed only in Constructors.
2. Must be first statements.
3.Either super() or this, but not both.

  • We can invoke another constructor from constructor from a method violation leads to CTE. i.e, super() or this must be used inside the constructor only not anywhere else.
Overloaded Constructors:
We are allowed to keep more than one constructor inside a class , which are considered as overloaded constructors. We can’t override the constructors, because they belong to the same Ex: class Test {
Test(int i){}
Test(){} ---->Overloaded Constructors
}
  • Constructors are not inherited and hence we are not allowed to override a constructor.
  • while recursive method invocation we will get stackoverflowException But in case of constructors we will get compile time error.
  • If we are writing any constructor in our class it is recommended to place default constructor also. Otherwise we should take care while writing the constructor child case.
  • If the parent class constructor throws some Checked Exception, while writing child class constructors we should take care.In case of unchecked exception no rule.
Recursive Constructor invocation:
class Sample {
Sample() // This is a compile time problem
{
this(10);
}
Sample(int i)
{
this(); // Invalid, CTE: recursive constructor invocation
}
public static void main(String a[])
{
System.out.println(“hai”);
}
}
Constructing the Child class constructors:
Example:
class P {
P()
{ super(); }
}
class C extends P
{
C() {
super();
} } //valid

Example:
class P {
P(int i)
{
System.out.println(i);
}
}
class C extends P
{
C()
{
super(10); // valid (without super invalid)
}
}

Example: class P
{
P() throws Exception ----> checked exception
{ }
}
class C extends P
{
C() // compile time error unhandled exception type exception
{ }
}



0 comments:

Post a Comment