Constructor in Java
Constructor in Java in this tutorial is explain about Constructor in Java and also explain about type of constructor, default and parameterized constructor with explanation and given example of Java Programming.
Constructor:-
Syntax of constructor:-
class class_name{
class_name(){
}
}
Type of a Constructor:-
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Private Constructor
Most Read:- Introduction of Java
1. Default Constructor:-
In this constructor does not passing any parameter and that is call at compile time is called a default constructor. If you have not defined a constructor in a class and you create a object of the class in a main class then its automatically called and initialize the value.
Example of default constructor:- if you create a default constructor then :-
class Student{
int mRollno;
String mName;
double mMarks;
Student() // default constructor
{
mRollno=1;
mName="Gourav";
mMarks=90.10;
}
public void display(){
System.out.println(mRollno+ " "+ mName+" "+ mMarks);
}
}
class Main{
public static void main(String [] args){
Student s = new Student();
s.display();
}
}
Output:- 1 Gourav 90.10
If you do not create the default constructor then compiler is automatically create a default constructor and initialize the default value in a constructor class.
output is print :- null null null
2. Parameterized Constructor:-
In this constructor we are passing a one and more parameters in a constructor is called a Parameterized constructor.
Example of Parameterized Constructor:-
class Student{
int mRollno;
String mName;
double mMarks;
Student() // default constructor
{
mRollno=1;
mName="Gourav";
mMarks=90.10;
}
Student(int sRollno, String sName) // prameterized constructor
{
mRollno=sRollno;
mName=sName;
System.out.println(mRollno+ " "+ mName)
}
Student(int sRollno, String sName, double sMarks) // prameterized constructor
{
mRollno=sRollno;
mName=sName;
mMarks=sMarks;
System.out.println(mRollno+ " "+ mName+" "+ mMarks)
}
public void display(){
System.out.println(mRollno+ " "+ mName+" "+ mMarks);
}
}
class Main{
public static void main(String [] args){
Student s = new Student();
s.display();
Student s1 = new Student(2, "Raj");
Student s2 = new Student(3, "Abishek", 95.59);
}
}
Most Read:- what is abstraction and used abstract class & method in Java?
Output:- 1 Gourav 90.10
2 Raj
3 Abishek 95.59
3. Copy Constructor:-
constructor that passing a reference of object is called a copy constructor.
Syntax of copy constructor :-
class class_name{
class_name(object ref){
Statement.........
}
}
Example of copy constructor:-
class Student{
int mRollno;
String mName;
double mMarks;
Student() // default constructor
{
mRollno=1;
mName="Gourav";
mMarks=90.10;
System.out.println(mRollno+ " "+ mName+" "+ mMarks);
}
Student(Student ref) // copy constructor
{
mRollno=ref.mRollno;
mName=ref.mName;
mMarks=ref.mMarks;
System.out.println(mRollno+ " "+mName+" "+mMarks);
}
}
class Main{
public static void main(String [] args){
Student s = new Student();
Student s1= new Student(s);
}
}
Output:- 1 Gourav 90.10
1 Gourav 90.10
4. Private Constructor:-
If you defined a constructor as a private you cannot access out side of the class.
Example of Private Constructor:-
class Student{
int mRollno;
String mName;
double mMarks;
private Student() // private constructor
{
mRollno=1;
mName="Gourav";
mMarks=90.10;
System.out.println(mRollno+ " "+ mName+" "+ mMarks);
}
public static void main(String [] args){
Student s = new Student();
}
}
What is a constructor overloading:-
Constructor Overloading:- When a create a more then one constructor with the same name of class but its passing parameters is different, In such a way each constructor perform a different task is called constructor overloading.
Syntax:-
class DemoOverloding{
DemoOverloding(){
}
DemoOverloding(int a){
}
DemoOverloding(float b){
}
DemoOverloding(int a, float b){
}
}
Example:-
class Student{
int mRollno;
String mName;
double mMarks;
Student() // default constructor
{
this.mRollno=1;
this.mName="Gourav";
this.mMarks=90.10;
}
Student(int mRollno, String mName, double mMarks) // prameterized constructor
{
this.mRollno=mRollno;
this.mName=mName;
this.mMarks=mMarks;
}
public void display(){
System.out.println(mRollno+ " "+ mName+" "+ mMarks);
}
}
class Main{
public static void main(String [] args){
Student s = new Student();
s.display();
Student s1= new Student(2, "Raj", 98.56);
s1.display();
}
}
Output:- 1 Gourav 90.10
2 Raj 98.56
Most Read:- Function in Java and call by value and call by reference in methods?
0 Comments
Hello Friends, Please comment and share and do not write spam messages