S.VIGNESH Assistant Professor/CSE, SECE Constructor in Java 01-01-2019 S.VIGNESH, AP/CSE
What is Method Constructor? Constructor is a special type of method that is used to initialize the object. Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor. 01-01-2019 S.VIGNESH, AP/CSE
Rules for creating constructor There are basically two rules defined for the constructor. Constructor name must be same as its class name Constructor must have no explicit return type 01-01-2019 S.VIGNESH, AP/CSE
Types of constructors There are two types of constructors: Default constructor (no-arg constructor) Parameterized constructor Note: If there is no constructor in a class, compiler automatically creates a default constructor. 01-01-2019 S.VIGNESH, AP/CSE
1. Default Constructor A constructor that have no parameter is known as default constructor. Syntax of default constructor: <class_name>(){} Example: class Bike{ Bike(){ System.out.println("Bike is created");} public static void main(String args[]){ Bike b=new Bike(); } Output: Bike is created 01-01-2019 S.VIGNESH, AP/CSE
Purpose of default constructor? Default constructor provides the default values to the object like 0, null etc. depending on the type. Example: class Student{ int id; String name; void display(){ System.out.println(id+" "+name);} public static void main(String args[]){ Student s1=new Student(),s2=new Student(); s1.display(); s2.display(); } Output : 0 null 01-01-2019 S.VIGNESH, AP/CSE
2. Parameterized Constructor Parameterized constructor is used to provide different values to the distinct objects. Syntax of Parameterized constructor: <class_name>(){ <class_name>(args1,…args n) } 01-01-2019 S.VIGNESH, AP/CSE
Example class Student{ } void display(){ 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(); } } Output: 111 Karan 222 Aryan 01-01-2019 S.VIGNESH, AP/CSE
Constructor Overloading Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists. The compiler differentiates these constructors by taking into account the number of parameters in the list and their type. 01-01-2019 S.VIGNESH, AP/CSE
Example Output: 111 Karan 0 222 Aryan 25 class Student{ int id; String name; int age; Student(int i,String n){ id = i; name = n; } Student(int i,String n,int a){ age=a; void display(){ System.out.println(id+" "+name+" "+age); } public static void main(String args[]){ Student s1 = new Student(111,"Karan"); Student s2 = new Student(222,"Aryan",25); s1.display(); s2.display(); } } Output: 111 Karan 0 222 Aryan 25 01-01-2019 S.VIGNESH, AP/CSE
JAVA CONSTRUCTOR JAVA METHOD It is used to initialize the state of an object. It is used to expose the behavior of an object. It must not have an return type. It must have return type. It is invoked implicitly. It is invoked explicitly. The java compiler provides a default 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. 01-01-2019 S.VIGNESH, AP/CSE
01-01-2019 S.VIGNESH, AP/CSE
Thank You 01-01-2019 S.VIGNESH, AP/CSE