Constructors:
a) A constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object
b) It can be used to initialize the objects, to required, or default values at the time of object creation
c) It is not mandatory for the coder to write a constructor for the class
Difference b/w programmer Written Code Compiler Generated Code a)
a) A constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object
b) It can be used to initialize the objects, to required, or default values at the time of object creation
c) It is not mandatory for the coder to write a constructor for the class
Rules for Constructors:
a) The name of the class and name of the Constructor must be same
b) Return type is not allowed for the Constructors even “void” also
c) If your placing any return type then compiler treats it as method we won’t get any compiler time error
Eg: Class jagan
{
Void jagan()
{
//It is a method but not constructor
}
}
Hence It is legal (Stupid) to a have method name is exactly same as Class name
Case1:
1. If you’re not writing any constructor then the compiler will always generate default Constructor
2. If you’re writing at least one constructor (customized) then compiler won’t generate any default constructor the programmer must should provide the constructors
Case2:
The only applicable modifiers for the constructors are:
a) Private c) protected
b) Default d) public
case3:
proto type of default constructors:
a) It is always no argument constructors.
b) It contains only one line super() ; It is call to super class constructor and it should be no argument call
Eg: Class jagan
{
Jagan()
{
Super ();
}
}
C) The Access modifiers of the default Constructors is same as class modifier
Case4:
About this() and super():
We can’t use super() or this() any where expect constructor otherwise getting Compile Time Error(CTE)
Class jagan
{
Public void mohan() Conclusion:
{ a) we have use only in constructor
this(); b) As the first Statement only
} c)but not simentenously
}
Case5:
Every class in java including abstract class also can contain “Constructors”
But interface cannot Contain Constructors
Class Test
{ abstract Class Test { interface T est
Test() Test() {
{ {//ok Test()
//ok } { //not ok
} } }
} }
Difference b/w programmer Written Code Compiler Generated Code a)
Programmer Written Code :
a)Class jagan
{
}
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
{
}
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
Compiler Generated Code:
a)Class jagan
{
super();
}
{
super();
}
Programmer Written Code :
b) Class jagan
jagan()
{
System.out.println("jagan mohan paspula");
}
}
Compiler Generated Code:
b) Class jagan
jagan()
{
super();
System.out.println("jagan mohan paspula");
}
}
Programmer Written Code :
c) Class jagan
{
void jagan()
{
}
}
Compiler Generated Code:
c) Class jagan
{
void jagan()
{
}
jagan()
{
super();
}
}
{
void jagan()
{
}
jagan()
{
super();
}
}
Programmer Written Code :
d) Class jagan
{
jagan(int i)
{
super();
System.out.println("Hai jagan mohan");
}
Compiler Generated Code:
<!--[if !supportLists]-->a) <!--[endif]-->no new need code is going to generated
Programmer Written Code :
e)Class jagan
{
jagan()
{
this(10);
system.out.println("Hai jagan ");
}
jagan(int i)
{
System.out.println("mohan")
}
}
Compiler Generated Code:
e)Class jagan
{
jagan()
{
this(10);
system.out.println("Hai jagan ");
}
jagan(int i)
{
super();
System.out.println("mohan")
}
}
Recursive method calls:
It is always runtime exceptions saying stack over flow “error”
Class Test
{
Public static void m1()
{
m2 ();
}
Public static void m2 ()
{
m1();
}
Public static void main(String args[])
{
System.out.println(“Hello jagan mohan paspula”);
m1(); “RE”
}
}//stack over flow Error
Recursive Constructor invocation a“CTE”
Case1: class Test
{
Test()
{
this(10);
}
Test(int i)
{
this();
}
Public static void main(String args[])
{
System.out.println(“Hello”);
}
}
Case 2:
Class p class p
{ {
P()
} {
}
Class c extends p Class c extends p
{ {
}
}
Class p extends c
{
P(int i)
{
}
Class c CTE:p(int p)canot be applied to here should not p() no argument
{ here should not be terminated both constructor
C() “Conculsion”:If the parent class has some constructor then while child
{
//Super(); Constructor we should take care
} Conculsion:wher ever writing argument constructor it is recommended to place no
} argument constructor also
COMMING SOON:
1.constructor over loading
2.useage of this() and super()
3.what type variable intilization possiable in constructors
//.Static control flow -->(HOT)
//.Static control flow -->(HOT)