6:CONSTRUCTORS




Ø It is a method or a block of code whose name is same as “class name”.
Ø Constructor is used to create an object of a class.
E.g.:-Discuss d=new Discuss ();
Ø If we define any constructor inside a java file then the compiler will consider that,
Otherwise compiler will creates a default constructor inside the JVM.
Ø If we are able to create an object of a class we have not define a constructor.

They are two types of constructors are there.
1.   Default constructor
2.   Parameterized constructor
Default constructor:-
If a constructor does not have any parameter, it is called as “default constructor”.
class Discuss {
int  a,  j;
Output:-
Create object of a class
demonstration

Discuss ( )  {
System.out.println ("create object of a class");    }
void demonstration ( ) {
System.out.println ("demonstration");        }
public static void main (String [] args)  {
Discuss d=new Discuss ();
d.demostration ();
}   }

Parameterized constructors:-
If a constructor has 1 or more parameters, it is called as “parameterized constructor”.
E.g.:-
class Const {
int   x,y,z;
Const ( )  {
System.out.println (“default constructor”);    }
Const (int x) {
this.x=x;
System.out.println(x);    }
Const (int x, int y) {
this.x=x;

Output:-
default constructor
2
2
3
2

this.y=y;
System.out.println(x);
System.out.println(y);    }
Const (int x, int y, int z) {
this.x=x;
this.y=y;
this.z=z;
System.out.println(x);
System.out.println(y);
System.out.println (z);     }
public static void main (String [] args) {
Const c = new Const ( );
Const c1= new Const (2);
Const c2 = new Const (2, 3);
Const c3 = new Const (2, 3, 4);   }  }

 “this” keyword is used to represent the current class instance variable(non static).




 “ this “   keyword:
Ø This which is used to represent the current class instance variables.
Ø “this” is used to represent the current object of the current class.
Ø Parameterized constructors are used to initialize the instance variables (non static).
Ø This operator is also used for calling the current class constructor.
Ø This can be called only from nonstatic blocks of a class.  E.g.:-
public class Myclass {

Output:-
6
8
6

int   i,  j;
Myclass()   {
this (6);     }
Myclass(int i)   {
this (6, 8);
System.out.println (i);    }
Myclass(int i, int j)   {
System.out.println (i);
System.out.println (j);    }
public static void main (String [] args)   {
Myclass m=new Myclass ();      }   }



Explanation:-

The execution starts from main ().the 1st statement in the main () is “new myclass ()” then it will creates a memory for nonstatic contents of a class and myclass () is loaded and executed.

1.   The this (6) will be loaded and it calls one parameterized constructor myclass (int i), then it will be loaded.
2.   The this (6, 8) will be loaded and it calls two parameterized constructor myclass (int i, j), then it will be loaded and executed and deleted from Ram.
3.   And it will again goto myclass (int i) and prints 6 and deleted from Ram.
Finally it gives result as
6    8     6.    
Note:-
Ø Constructor is a non static block, we cannot define a constructor as static constructor.
“Static myclass” gives a compiler error as
Modifier static is not allowed here.
Ø Constructor has to be loaded while creating an object so, it should not be static.

Constructor Crated by compiler and programmer:-  
Class A    {    }
Class B extends A    {     }
Constructor by Programmer
Constructor BY Compiler
B(int x)  {


}
B(int x)  {
super( );

}

B(  ) {


}


B( ) {
super();

}

B(int x){
this();

}
B(int x)  {
super();
this();      }
Not provided any constructor in B
B( )  {
Super();
}
Default constructor will be created by compiler.
Inheritance
Concept of accessing properties and behaviors of object of one class from object of another class. “Extends “is a keyword which supports the concept of inheritance.
[Class Y extends X]
Here,   Y is extending class or subclass or child class.
           X is extended class or super class or parent class.
E.g.:-
class X   {          // parent class   (or)  /super class   
int i;
void  funX ( )    {
System.out.println (i);
}    }
// Y sub class (child)
class Y extends  X    {
int   j;
void  funY ( )    {
System.out.println (j); }
public static void main (String [] args)   {
Y y = new Y ();
System.out.println (y.j);
y.i = y.i+44;
System.out.println (y.i);
y.funX ();
y.funY ();    }    }
Explanation:-
Ø When we starting execution from Y the jvm will starts execution from main(),first statement inside main() is Y y = new Y();
Ø When new operator encounters jvm will allocate the memory for non static contents of class Y.
Ø Since we are calling Y() will be loaded ,jvm will starts execution from constructor Y();
Ø While execution of Y () jvm will checks whether class Y was extending any other class or not.
Ø If yes, it will creates object of that class which is extended (class X) in association with object of class Y.
Ø So,’y’ is a reference variable which will have access to both Xobject and Y object non static contents.so, we can access the variables of classX using Y object.

Ø E.g.:- y.i = y.i+44;
y.funX ();
y.funY ();

Ø if  jvm finds extends keyword when we create an object of subclass ,object will be created for subclass in association with its super class object.
“ super “   keyword:-
Super is a keyword which can be used to access the non static contents of immediate super class of a current class.     E.g.:-
Output:-
0
0
200
110
110
0

class superclass   {
int a=10;
void  functiona ( )   {
System.out.println (a);    }    }
class Super extends superclass   {
int  a;
int  b;
void  functiona( )   {
System.out.println (a+b+200);   }
void  functionb ( )   {
super. a=super.a+100;
super.functiona( );
System.out.println (super. a);
System.out.println (b);    }
public static void main (String [] args) {
super s=new Super ( );
System.out.println (s.a);
System.out.println (s.b);                      //s.functionb ();
s.functiona( );
s.functionb ( );   }     }
Example:-
class A   {
int a = 10;
A( )   {
Output  :
def constructor of A

System.out.println("def constructor of A");
}
void  funA ( )   {
System.out.println (a);
}    }
class B extends A   {
     /*   B ()   {
          Super ();
       System.out.println ("def Construct of B");   }    */
int a ;
int b ;
public static void main(String[] args)   {
B bObj = new B();
}     }
Explanation:-       B bObj = new B();
When this stmt is executed B() class default constructor will be loaded.
Here we have to call to super() constructor ,1st super class constructor will be called it will create object of A in association with object of class B.   The scenario is as follows
When  JVM  loads B() since B extends A compiler will have a similar code as shown below
B()   {
super();    }

So,
When we create above statement the msg printed inside the A class constructor will become the output.
This is also an additional responsibility of a compiler to call the super class constructor inside the default constructor of current class, when it is extending other class.




No comments:

Post a Comment