constructors
------------
A constructor is a special method which is having same name as
classname.
A constructor doesn't have return type.
A constructor is used to initialize instance variables of a class.
A constructor is automatically called whenever an object is created.
Types of constructors
---------------------
i)Default.
ii)Parameterized.
default
-------
A constructor without parameters is known as default constructor.
syntax
------
class ClassName
{
//statements
acessmodifier ClassName()
{
//initialization code
}
//statements
}
ex:
public Car()
{
//statements
}
Program
-------
Car -->name.
-->color.
-->price.
-->cno.
Parameterized constructor
-------------------------
A constructor with parameters is known as parameterized constructor.
syntax
------
class ClassName
{
//statements
accessmodifier ClassName(datatype var1,datatype var2,...)
{
}
//statements
}
Program
-------
class Car
{
String name;
String color;
double price;
int cno;
//constructor
public Car(String n,String c,double p,int no)
{
name=n;
color=c;
price=p;
cno=no;
}
public void show()
{
System.out.println(name);
System.out.println(color);
System.out.println(price);
System.out.println(cno);
}
public static void main(String args[])
{
Car c1=new Car("bmw","white",100000.00,7777);
Car c2=new Car("audi","white",100000.00,7777);
c1.show();
c2.show();
}
}
An object declaration consists of 2 parts
i)refernce part
ii)Object part
ex:
Car c = new Car("bmw","white",100000.00,777);
reference object part
part
An object consists of 2 parts
i)new operator
ii)Constructor calling
new operator allocates memory for object.
constructor calling is implicit part of object.
If we have multiple constructors in a class,we should creat a seperate object to invoke(call) each constructor of a class.
Program
-------
class Car
{
String name;
String color;
double price;
int cno;
//default
public Car()
{
name="lambo";
color="white";
price=20000.00;
cno=5555;
}
//Parameterized constructor
public Car(String n,String c,double p,int no)
{
name=n;
color=c;
price=p;
cno=no;
}
public void show()
{
System.out.println(name);
System.out.println(color);
System.out.println(price);
System.out.println(cno);
}
public static void main(String args[])
{
Car c1=new Car();//line1
Car c2=new Car("bmw","white",100000.00,7777);//line2
c1.show();
c2.show();
}
}
line1--is invoking default constructor.
line2--is invoking parameterized constructor.
Declaring multiple constructors is known as constructor overloading.
Constructor overloading gives an opportunity for the developer to choose different ways of creating object,based on requirement developer can choose the required constructor while instantiating a class.
[will discuss more about overloading in polymorphism]
If developer is not including any constructor in a class then the compiler will automatically include a default constructor to a class which is also called system default constructor,that is why we don't get any compilation error while making a call to default constructor even though we don't declare any constructor in a class.
Program
-------
class Car
{
String name;
String color;
double price;
int cno;
public void show()
{
System.out.println(name);
System.out.println(color);
System.out.println(price);
System.out.println(cno);
}
public static void main(String args[])
{
Car c1=new Car();
c1.show();
}
}
Analysis
--------
In the above code we didn't declare any constructor in Car class but we are making a call to default construtor,here compiler will automatically add one default constructor to a class.
A system default constructor will initialize instance variables to default values if developer is not initializing instance variables.
Program
-------
class Test{
{
System.out.println("hello");
}
static
{
System.out.println("hi");
}
public Test()
{
System.out.println("hiiiiiii");
}
public static void main(String args[])
{
System.out.println("GM");
Test t=new Test();
System.out.println("GN");
}
}
output
------
hi
GM
hello
hiiiiii
GN
Note
----
Redeclaration of anything is not possible in java within same scope except for instance and static blocks.
what is the output of the following code?
class Test{
{
System.out.println("hello");//5
}
static //1
{
System.out.println("hi");
}
public Test(int x,int y)//7
{
System.out.println("hiiiiiii");
}
public static void main(String args[])//3
{
Test t=new Test(10,20);//4
}
static //2
{
System.out.println("Prabhas");
}
{//6
System.out.println("Pawan");
}
}
output
------
hi
prabhascon
hello
pawan
hiiiiii
A constructor has same name as -------
constructor doesn't have ------
constructor iniatizes -------
constructor is automatically called when-------
default constructor doesn't have-----
Parameterized constructor doesn't have-----
Advanatge of construtor?
instance block or constructor ----is called first.
------------
A constructor is a special method which is having same name as
classname.
A constructor doesn't have return type.
A constructor is used to initialize instance variables of a class.
A constructor is automatically called whenever an object is created.
Types of constructors
---------------------
i)Default.
ii)Parameterized.
default
-------
A constructor without parameters is known as default constructor.
syntax
------
class ClassName
{
//statements
acessmodifier ClassName()
{
//initialization code
}
//statements
}
ex:
public Car()
{
//statements
}
Program
-------
Car -->name.
-->color.
-->price.
-->cno.
Parameterized constructor
-------------------------
A constructor with parameters is known as parameterized constructor.
syntax
------
class ClassName
{
//statements
accessmodifier ClassName(datatype var1,datatype var2,...)
{
}
//statements
}
Program
-------
class Car
{
String name;
String color;
double price;
int cno;
//constructor
public Car(String n,String c,double p,int no)
{
name=n;
color=c;
price=p;
cno=no;
}
public void show()
{
System.out.println(name);
System.out.println(color);
System.out.println(price);
System.out.println(cno);
}
public static void main(String args[])
{
Car c1=new Car("bmw","white",100000.00,7777);
Car c2=new Car("audi","white",100000.00,7777);
c1.show();
c2.show();
}
}
An object declaration consists of 2 parts
i)refernce part
ii)Object part
ex:
Car c = new Car("bmw","white",100000.00,777);
reference object part
part
An object consists of 2 parts
i)new operator
ii)Constructor calling
new operator allocates memory for object.
constructor calling is implicit part of object.
If we have multiple constructors in a class,we should creat a seperate object to invoke(call) each constructor of a class.
Program
-------
class Car
{
String name;
String color;
double price;
int cno;
//default
public Car()
{
name="lambo";
color="white";
price=20000.00;
cno=5555;
}
//Parameterized constructor
public Car(String n,String c,double p,int no)
{
name=n;
color=c;
price=p;
cno=no;
}
public void show()
{
System.out.println(name);
System.out.println(color);
System.out.println(price);
System.out.println(cno);
}
public static void main(String args[])
{
Car c1=new Car();//line1
Car c2=new Car("bmw","white",100000.00,7777);//line2
c1.show();
c2.show();
}
}
line1--is invoking default constructor.
line2--is invoking parameterized constructor.
Declaring multiple constructors is known as constructor overloading.
Constructor overloading gives an opportunity for the developer to choose different ways of creating object,based on requirement developer can choose the required constructor while instantiating a class.
[will discuss more about overloading in polymorphism]
If developer is not including any constructor in a class then the compiler will automatically include a default constructor to a class which is also called system default constructor,that is why we don't get any compilation error while making a call to default constructor even though we don't declare any constructor in a class.
Program
-------
class Car
{
String name;
String color;
double price;
int cno;
public void show()
{
System.out.println(name);
System.out.println(color);
System.out.println(price);
System.out.println(cno);
}
public static void main(String args[])
{
Car c1=new Car();
c1.show();
}
}
Analysis
--------
In the above code we didn't declare any constructor in Car class but we are making a call to default construtor,here compiler will automatically add one default constructor to a class.
A system default constructor will initialize instance variables to default values if developer is not initializing instance variables.
Program
-------
class Test{
{
System.out.println("hello");
}
static
{
System.out.println("hi");
}
public Test()
{
System.out.println("hiiiiiii");
}
public static void main(String args[])
{
System.out.println("GM");
Test t=new Test();
System.out.println("GN");
}
}
output
------
hi
GM
hello
hiiiiii
GN
Note
----
Redeclaration of anything is not possible in java within same scope except for instance and static blocks.
what is the output of the following code?
class Test{
{
System.out.println("hello");//5
}
static //1
{
System.out.println("hi");
}
public Test(int x,int y)//7
{
System.out.println("hiiiiiii");
}
public static void main(String args[])//3
{
Test t=new Test(10,20);//4
}
static //2
{
System.out.println("Prabhas");
}
{//6
System.out.println("Pawan");
}
}
output
------
hi
prabhascon
hello
pawan
hiiiiii
A constructor has same name as -------
constructor doesn't have ------
constructor iniatizes -------
constructor is automatically called when-------
default constructor doesn't have-----
Parameterized constructor doesn't have-----
Advanatge of construtor?
instance block or constructor ----is called first.
No comments:
Post a Comment