Sunday, 17 July 2011

Constructor



Constructor:-
Constructor is a special function that has same name as the name of the class. It doesnot return any value even void.
class A
{
A()
{
}
}

In a class more than one constructor is possible. It means constructor overloading is possible.
A constructor without any argument is known as no-argument constructor.
A constructor with number of argument is known as parameterised constructor.
class A
{
A()                          //no argument constructor
{
}
A(int i)                  //parameterised constructor
{
}
}

*When no constructor is specified then compiler generates a constructor that is known as default constructor.
Default constructor has same access modifier as of the class.
Default constructor is always no argument constructor but vice-versa is not true.
Eg.
public class A
{
public A()   //same access modifier as of class
{
}
}

A constructor get executed as we create a object for the class.
A a=new A();
This will call no argument constructor otherwise default constructor.

*Some time class is public and no argument constructor is default then it is not accessible all over.
Eg.
class A
{
A(int i)
{
}
}
class B
{
public static void main(String[] arg)
{
A a=new A();
}
}
This is will show error.
As default constructor is created when there is no constructor but there is constructor and its argument doesnot matches. So it will show error.

*Work of constructor is to initialise variables and to allocate memory.

Constructor & inheritance/ Chaining of Constructor:-
class A
{
A()
{
}
}
class B extends A
{
B()
{
}
}
Class C
{
public static void main(String[] arg)
{
B b=new B();
}
}

Note:-
The first of any constructor of any constructor must be super() or this(). If we doesnot specify then by default (compiler generates) super statement occurred & super will call to the super class constructor.

Note:-all the things written in red are generated by compiler and actually they are not written in the program.
Eg.
Class A
{
A()
{
super();
}
}
Class B extends A
{
B()
{
super();
}
}
The super of B will call the constructor of A class and the super of A class will call the default constructor of Object class.
So firstly constructor A will execute then of B.

Eg.
class A
{
A()
{
System.out.println(“welcome A”);
super();
}
}
This will show error as super() can only be the first statement.

Eg.
class A
{
A(int i)
{
}
}
Class B
{
B()
{
super();
}
}
This will show error as super of B will call the no argument constructor of B but the argument doesn’t matches.

To correct it we have to pass our self in value in super
Eg.
class A
{
A(int i)
{
}
}
Class B
{
B()
{
Super(5);
}
}

this:- will call current class constructor

class A
{
A()
{
this(5);
}
A(int i)
{
super();            //generated by compiler
}
}

A a=new A();
This will call the no argument constructor of A which will further call the A(int i) constructor the super of which will further call the default constructor of Object class.

Eg.
class A
{
A()
{
super();
}
A(int i)
{
this();
}
}

A a=new A(5);
This will call the A(int i) constructor & the this() of which will call the no argument constructor. The super of no argument constructor will call the default constructor of Object class.

*
class A
{
A(int i)
{
this();
}
}
A a=new A(5);
This will show error.

*class A
{
A()
{
this(5);
}
A(int i)
{
this()
}
}
A a=new A(5);
This will show stack overflow exception
In this case firstly A(int i) is called. The this of which will call A().
The this(5) will again call A(int i) this process is continued so show exception.

*class A
{
A(int i)
{
this(5);
}
}
A a=new A(5);
This will show stack overflow error.

*class A
{
A()
{
this()
{
}
}
A a=new A();
This will show stack overflow error.

Note:-
Local variable are preferred over member variable.
This is used to refer current reference of the class. It is used to differentiate between local & member variable.

*class Employee
{
int salary;
Employee(int salary)
{
this.salary=salary;
}
}
Employee e=new Employee(1000);

In this case this.salary is member variable where as salary is local variable.

*class Person
{
}
class Employee extends Person
{
int salary;
Employee(int salary)
{
this.salary=salary;
display(this);
}
void display(Person a)
{
}
}

display(this) will call the display function of same class.
Example:-
class this30
{
this30()
{
this(5);
System.out.println("rohtak");
}
this30(int i)
{
System.out.println("haryana");
}
}
class this30main
{
public static void main(String[] arg)
{
this30 t=new this30();
}
}

Special case:-
class person
{
void display()
{
System.out.println("Display person");
}
void input()
{
System.out.println("Input");
}
}
class employee extends person
{
void display()
{
System.out.println("display employee");
}
void getdata()
{
System.out.println("GET DATA");
}
}
class jun6main
{
public static void main(String[] arg)
{
employee e=(employee)new person();
//type casting
//e.display();
}
}

Jagged Array:-
Number of element in each array may be different.

No comments:

Post a Comment

 

About The Author


I am a B.Tech Computer Science Student. I love Programming and blogging. I am Oracle certified Java Professional. Read More
Indian Blogger

Teachers Jobs

Footer 3

Twitter Facebook RSS YouTube Google
​

All Rights Reserved ©Sun Java Masters 2013