Constructor in java

2,100 views 12 slides Nov 26, 2020
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

In this you learn about
--Constructors in Java
--Types of Constructors
1. Default Constructor
2. Parameterized Constructor
Difference between Constructor and Method


Slide Content

JAVA Constructors

Topics for Today’s Session Constructor Difference between Method and Constructor Types of Constructors

Constructors A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory. Every time an object is created using the new() keyword, at least one constructor is called. Rules for creating Java 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

Types of Java constructors There are two types of constructors in Java: Default constructor (no-arg constructor) Parameterized constructor

Default Constructor 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. class Bike1{   Bike1(){ System.out.println("Bike is created"); }   public static void main(String args[]){   Bike1 b=new Bike1();   }  } Syntax : <class_name>(){ }  

// Create a MyClass 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

Example of default constructor 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();   }  }   Output 0 null 0 null 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.

Parameterized constructor Constructors that 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();
} }

Difference between constructor and 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.

// Using Constructor class Car { 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

Summary In this lesson you learnt about Constructor Types of constructors Difference between Constructor and Method. SHARE