Presentation is loading. Please wait.

Presentation is loading. Please wait.

S.VIGNESH Assistant Professor/CSE, SECE

Similar presentations


Presentation on theme: "S.VIGNESH Assistant Professor/CSE, SECE"— Presentation transcript:

1 S.VIGNESH Assistant Professor/CSE, SECE
Constructor in Java S.VIGNESH, AP/CSE

2 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. S.VIGNESH, AP/CSE

3 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 S.VIGNESH, AP/CSE

4 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. S.VIGNESH, AP/CSE

5 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 S.VIGNESH, AP/CSE

6 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 S.VIGNESH, AP/CSE

7 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) } S.VIGNESH, AP/CSE

8 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 S.VIGNESH, AP/CSE

9 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.   S.VIGNESH, AP/CSE

10 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 S.VIGNESH, AP/CSE

11 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. S.VIGNESH, AP/CSE

12 S.VIGNESH, AP/CSE

13 Thank You  S.VIGNESH, AP/CSE


Download ppt "S.VIGNESH Assistant Professor/CSE, SECE"

Similar presentations


Ads by Google