CSC 211 Lesson 4 Part3 Constructors
CSC 211 Lesson 4 Part3 Constructors
Mr. Tarus
Outline
Introduction
Types of Constructors.
Copy constructor.
this keyword
Constructors. ConstructDemo.java
A special type of method which is used to initialize the object.
Every time an object is created using the new() keyword, at least one constructor
is called.
It is called constructor because it constructs the values at the time of object
creation.
Java compiler creates a default constructor for every class created.
Rules for creating Java constructor.
Class name and constructor name must be the same.
Do not have return types.
A Java constructor cannot be abstract, static, final, and synchronized
Constructors can use access modifiers. (private, public, protected, or default)
Types of Constructors
2. Parameterized constructor.
3. Copy Constructor
Java Default Constructor. Main12.java, ConstructDemo.java
Constructors which does not have any parameters in the argument list.
Syntax: <class_name>(){}
//Java Program to create and call a default constructor
class Bike1{
//creating a default constructor
Bike1() {
System.out.println("Bike is created by constructor");
}
public static void main(String args[]){
Bike1 b=new Bike1(); //calling a default constructor
}
}
Java Parameterized Constructor
ParameterizedCon.java
student4.java
Constructor Overloading in Java. OverloadedCons.java
Technique of having more than one constructor with different parameter lists.
The compiler differentiates them based on the number of parameters and their
return types.
// overload constructors void display(){
class OverloadedCons{ System.out.println(id+"
int id; "+name+" "+age);
String name; }
int age;
//creating two arg constructor public static void main(String
args[]){
OverloadedCons(int i,String n){
OverloadedCons s1 = new
id = i; OverloadedCons(111,"Karan");
name = n; } OverloadedCons s2 = new
//creating three arg constructor OverloadedCons(222,"James",25);
OverloadedCons(int i,String n,int a){ s1.display();
id = i; s2.display();
name = n; }
age=a; } }
Difference between constructor and method in Java
Java Constructor Java Method
Used to initialize the state of an Used to expose the behavior of an
object. object.
Name must be same as the class Name may or may not be same as the
name. class name.
Copy Constructor. CopyConstructor.java
Copy constructor is passed with another object which copies the data available
from the passed object to the newly created object.
In Java, there is no such inbuilt copy constructor available like in other
programming languages such as C++.
In Java, we can create our own copy constructor by passing the object of the
same class to the other instance(object) of the class.
this keyword
• reference variable that refers to the current object.
Usage of Java this keyword
Used to refer current class instance variables.
this can be used to return the current class instance from the method.
NoThisStudentDemo.java, ThisStudentDemo.java
this: to invoke current class method. Athis.java
You may invoke the method of the current class by using the this keyword. If
you don't use the this keyword, compiler automatically adds this keyword
while invoking the method. Let's see the example
this() : to invoke current class constructor. TestThis5.java
The this() constructor call can be used to invoke the current class constructor. It
is used to reuse the constructor. In other words, it is used for constructor
chaining.
Calling parameterized constructor from default constructor:
• TestThisE.java
Real usage of this() constructor call
The this() constructor call should be used to reuse the constructor from the
constructor. It maintains the chain between the constructors i.e. it is used for
constructor chaining. Let's see the example given below that displays the actual
use of this keyword.
TestThisDemo.java