CONSTRUCTOR
S.G.V.P.Gunasekara - 2015238
1
Constructor
Constructor in java is a special type of method that is used to initialize the
object.
Java constructor is invoked at the time of object creation. It constructsthe
values i.e. provides data for the object that is why it is known as
constructor.
2
Rules for creating java constructor
There are basically two rules defined for the constructor.
1. Constructor name must be same as its classname
2. Constructor must have no explicit returntype
3
Types of java constructors
There are two types of constructors:
1. Default constructor (no-arg constructor)
2. Parameterizedconstructor
4
Default constructor
class. It will be invoked at the
A constructor that have no parameter is known as defaultconstructor.
Syntax of default constructor:
<class_name>(){}
Example of default constructor
In this example, we are creating the no-arg constructor in theBike
time of object creation.
class Bike1{
Bike1(){
System.out.println(&quot;Bike is created&quot;)
;}
public static void main(Stringargs[]){
Bike1 b=new Bike1();
}
}
5
Parameterized constructor
A constructor that have parameters is known as parameterizedconstructor.
Why use parameterized constructor?
Parameterized constructor is used to provide different values to
the distinct objects.
6
 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 Student4{
int id;
String name;
Student4(int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+&quot; &quot;+name);}
public static void main(String args[]){
Student4 s1 = new Student4(111,&quot;Karan&quot;);
Student4 s2 = new Student4(222,&quot;Aryan&quot;);
s1.display();
s2.display();
}
}
7
Output:
111 Karan
222 Aryan
8
Constructor overloading
Like other methods in java constructor can be overloaded i.e. we can
create asmany constructors in our class asdesired. Number of
constructors depends on the information about attributes of an objectwe
have while creating objects.
9
example: constructor overloading example:
classLanguage{
String name;
Language() {
System.out.println("Constructor methodcalled.");
}
Language(String t) {
name = t;
}
public static void main(String[] args) {
Language cpp = new Language();
Language java = new Language("Java");
cpp.setName("C++");
java.getName();
cpp.getName();
}
void setName(String t) {
name = t;
}
void getName() {
System.out.println("Language name: " + name);
}
}
10
Output:
Constructor method called.
Language name: java
Language name:c++
11
12

Constructor oopj

  • 1.
  • 2.
    Constructor Constructor in javais a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It constructsthe values i.e. provides data for the object that is why it is known as constructor. 2
  • 3.
    Rules for creatingjava constructor There are basically two rules defined for the constructor. 1. Constructor name must be same as its classname 2. Constructor must have no explicit returntype 3
  • 4.
    Types of javaconstructors There are two types of constructors: 1. Default constructor (no-arg constructor) 2. Parameterizedconstructor 4
  • 5.
    Default constructor class. Itwill be invoked at the A constructor that have no parameter is known as defaultconstructor. Syntax of default constructor: <class_name>(){} Example of default constructor In this example, we are creating the no-arg constructor in theBike time of object creation. class Bike1{ Bike1(){ System.out.println(&quot;Bike is created&quot;) ;} public static void main(Stringargs[]){ Bike1 b=new Bike1(); } } 5
  • 6.
    Parameterized constructor A constructorthat have parameters is known as parameterizedconstructor. Why use parameterized constructor? Parameterized constructor is used to provide different values to the distinct objects. 6
  • 7.
     Example ofparameterized 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 Student4{ int id; String name; Student4(int i,String n){ id = i; name = n; } void display(){System.out.println(id+&quot; &quot;+name);} public static void main(String args[]){ Student4 s1 = new Student4(111,&quot;Karan&quot;); Student4 s2 = new Student4(222,&quot;Aryan&quot;); s1.display(); s2.display(); } } 7
  • 8.
  • 9.
    Constructor overloading Like othermethods in java constructor can be overloaded i.e. we can create asmany constructors in our class asdesired. Number of constructors depends on the information about attributes of an objectwe have while creating objects. 9
  • 10.
    example: constructor overloadingexample: classLanguage{ String name; Language() { System.out.println("Constructor methodcalled."); } Language(String t) { name = t; } public static void main(String[] args) { Language cpp = new Language(); Language java = new Language("Java"); cpp.setName("C++"); java.getName(); cpp.getName(); } void setName(String t) { name = t; } void getName() { System.out.println("Language name: " + name); } } 10
  • 11.
    Output: Constructor method called. Languagename: java Language name:c++ 11
  • 12.