CONSTRUCTORS
• A constructorin java is a special method that is used to initialize objects.
• The constructor is called when an object of a class is created.
• In java, a constructor is a block of codes similar to the method. It is called
when an instance of the class is created.
• Every time an object is created using the new() keyword, at least one
constructor is called.
3.
Rules for creatingjava constructor
• Constructor name must be the same as its class name.
• A constructor must have no explicit return type.
• A java constructor cannot be abstract, static, final, and
synchronized
CONSTRUCTORS
4.
There are twotypes of constructors in java:
1. Default constructor (no-arg constructor)
2. Parameterized constructor
Types of
Constructors
Default Constructor
Parameterized
Constructor
TYPES OF JAVA CONSTRUCTORS
5.
Default constructor
class Bike1{
Bike1(){
System.out.println("Bikeis created");
}
public static void main(String args[]){
Bike1 b=new Bike1();
} }
Syntax :
<class_name>(){ }
• A constructor that have no parameter is known as default constructor.
• Default constructor provides the default values to the object like 0, null etc. depending
on the type.
• If there is no constructor in a class, compiler automatically creates a default
constructor.
6.
// Create amyclass class
class myclass {
int n; // create a class attribute
// Create a class constructor for the myclass class
public myclass() {
n = 10; // set the initial value for the class attribute n
}
public static void main(String[] args)
{
myclass myobj = new myclass(); // create an object of class myclass // ( this will call the
constructor)
System.Out.Println(myobj.n); // print the value of n
}
}
// Outputs
10
7.
Example of defaultconstructor that displays the default values
class student{
int id;
string name;
void display()
{ System.Out.Println(id+" "+name);
}
public static void main(string args[]){
student s1=new student();
student s2=new student();
s1.Display();
s2.Display();
} }
Explanation:
In the above class, you are not creating any constructor so compiler provides you a default constructor.
Here 0 and null values are provided by default constructor.
8.
Parameterized constructor
• Constructorsthat have parameters is known as parameterized constructor.
• Parameterized constructor is used to provide different values to the
distinct objects.
Example of parameterized constructor
/*In this example, we have created the constructor of Student class that have two parameters. We can
have any number of parameters in the constructor. */
class Student {
int id;
String name;
Student (int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[])
{
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
} }
9.
Difference between constructorand method in java
There are many differences between constructors and methods.
Constructor Method
Constructor is used to initialize the state
of an object.
Method is used to expose behaviour of
an object.
Constructor must not have return type. Method must have return type.
Constructor is invoked implicitly. Method is invoked explicitly.
The java compiler provides a default
constructor if you don't have any
constructor.
Method is not provided by compiler in
any case.
Constructor name must be same as the
class name.
Method name may or may not be same
as class name.
10.
// Using constructor
classcar {
int modyr;
String modname;
public car(int y, string n) {
modyr = y;
Modname = n;
}
Public static void main(string[] args) {
car mycar = new car(1969, "mustang");
System.Out.Println(mycar.Modyr + " " +
mycar.Modname);
}
}
// Outputs
1969
mustang
// Using mathods
class car {
int modyr;
String modname;
void getdata(int y, string n) {
modyr = y;
modname = n; }
void show(){
System.Out.Println(modyr);
System.Out.Println(modname);
}
public static void main(string[] args) {
car mycar = new car( );
mycar.getdata(1969, "mustang");
mycar.show();
}
}
// Outputs
1969
mustang
11.
class Car {
Stringmodel; int year;
// Default constructor
Car() {
model = "Unknown";
year = 0; }
// Parameterized constructor
Car(String x, int y)
{
model=x;
year = y; }
void display() {
System.out.println("Model: " + model + ", Year: " + year); }
public static void main(String[] args) {
Car car1 = new Car();
// Calls default constructor
Car car2 = new Car("Toyota", 2022);
// Calls parameterized constructor
car1.display();
car2.display(); } }
Constructor overloading