0% found this document useful (0 votes)
26 views10 pages

Divesh Ps Exp 4

The document discusses constructors and destructors in Java. It explains that a constructor is called when an object is created and is used to initialize the object. It describes default and parameterized constructors. It also covers constructor overloading and provides examples of using different constructors.

Uploaded by

hithj05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views10 pages

Divesh Ps Exp 4

The document discusses constructors and destructors in Java. It explains that a constructor is called when an object is created and is used to initialize the object. It describes default and parameterized constructors. It also covers constructor overloading and provides examples of using different constructors.

Uploaded by

hithj05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Experi

ment 04– constructor and destructor in java

Learning Objectives: Understand the concept of constructor and destructor

in java.

Theory:

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 the constructor, memory for the
object is allocated in the memory. It is 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 calls a default constructor if there is no constructor
available in the class. In such cases, Java compiler provides a default constructor by
default. There are two types of constructors in Java: 1) Default constructor 2)
Parameterized constructor.

Note: It is called constructor because it constructs the values at the time of object
creation. It is not necessary to write a constructor for a class. It is because java
compiler creates a default constructor if your class doesn't have any
Rules for creating constructor There are two rules defined for the constructor. 1.
Constructor name must be the same as its class name 2. A Constructor must have no
explicit return type 3. A Java constructor cannot be abstract, static, final, and
synchronized
Default Constructor A constructor is called "Default Constructor" when it doesn't have
any parameter.
Syntax of default constructor: (){}

//assign first element of an array to largest and smallest


int smallest = arr[0];
int largest = arr[0];

for(int i=1; i< arr.length; i++)


{
if(arr[i] > largest)
largest = arr[i];
else if (arr[i] < smallest)
smallest = arr[i];

}
Example of default constructor
1. Java Default Constructor
//Java Program to create and call a default constructor
class Bike1
{
//creating a default constructor
Bike1()
{
System.out.println("Bike is created");
}

//main method
public static void main(String args[])
{
//calling a default constructor
Bike1 b=new Bike1();
}}
Output: Bike is created

2.Java Parameterized Constructor: A constructor which has a specific


number of parameters is called a parameterized constructor. The
parameterized constructor is used to provide different values to distinct
objects. However, you can provide the same values also.
Example of parameterized constructor: In this example, we have
created the constructor of Student class that has two parameters. We
can have any number of parameters in the constructor.
//Java Program to demonstrate the use of the parameterized constructor.
class Student4
{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n)
{
id = i;
name = n;
}
//method to display the values
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[]){
//creating objects and passing values
Student s1 = new Student4(111,"Divesh");
Student4 s2 = new Student4(222,"Dive");
//calling method to display the values of object
s1.display();
s2.display();
}}
Output: 111 Divesh
222 Dive

Constructor Overloading in Java In Java, a constructor is just like a method but


without return type. It can also be overloaded like Java methods. Constructor
overloading in Java is a technique of having more than one constructor with different
parameter lists. They are arranged in a way that each constructor performs a different
task. They are differentiated by the compiler by the number of parameters in the list and
their types.
Example of Constructor Overloading
//Java program to overload constructors
class Student
{
int id;
String name;
int age;
//creating two arg constructor
Student(int i,String n)
{
id = i;
name = n;
}
//creating three arg constructor
Student(int i, String n, int a)
{
id = i;
name = n;
age=a;
}
void display()
{
System.out.println(id+" "+name+" "+age);
}
public static void main(String args[])
{
Student s1 = new Student(34 ,"Divesh" , 18);
Student s2 = new Student(35 ,"Ayush dubey", 18); s1.display();
s2.display();
}}
Output:
PS C:\Users\Divesh\OneDivee\Desktop\Divesh\java> cd "c:\Users\Divesh\OneDivee\Desktop\ayush\
java\" ; if ($?) { javac Student.java } ; if ($?) { java Student }
34 Divesh 18
35 Aayush dubey 18
*Designed a class SortData that contains the method asec() and desc().
import java.util.Scanner;
class pro {
Scanner input = new Scanner(System.in);
int num, i;
int arr[];
int temp = 0;
public void getdata() {
System.out.println("Enter length of array");
num = input.nextInt();
arr = new int[num];
System.out.println("Enter the number: ");
for (i = 0; i < num; i++)
{
arr[i] = input.nextInt();
}
}
void putdata() {
System.out.println("Given numbers are: ");
for (i = 0; i < num; i++)
{
System.out.print(" " + arr[i]);
}
}
void asce() {
for (i = 0; i < num; i++) {
for (int j = i + 1; j < num; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println(" ");
System.out.println("Ascending order of number are: ");
for (int i = 0; i < num; i++) {
System.out.print(" " + arr[i]);
}
}
void desc() {
for (i = 0; i < num; i++) {
for (int j = i + 1; j < num; j++) {
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println(" ");
System.out.println("Descending order of number are: ");
for (int i = 0; i < num; i++) {
System.out.print(" " + arr[i]);
}
}
}
public class Divesh {
public static void main(String[] args) {
pro obj = new pro();
obj.getdata();
obj.putdata();
obj.asce();
obj.desc();
}
}
output
PS C:\Users\Divesh\OneDivee\Desktop\Divesh\java> cd "c:\Users\Divesh\
OneDivee\Desktop\ayush\java\" ; if ($?) { javac Divesh.java } ; if ($?) { java
Divesh }
Enter length of array
5
Enter the number:
15 25 9 34 40
Given numbers are:
15 25 9 34 40
Ascending order of number are:
9 15 25 40 34
Descending order of number are:
34 40 25 15 9

Constructor Overloading in Java with examples


Like methods, constructor can also be overloaded. In this guide we will see Constructor
overloading with the help of examples. Before we proceed further let’s understand what
is constructor overloading and why we do it.
Constructor overloading is a concept of having more than one constructor with different
parameters list, in such a way so that each constructor performs a different task.
Constructor Overloading Example
Here we are creating two objects of class StudentData. One is with a default constructor and
another one using a parameterized constructor. Both the constructors have different initialization
codes, similarly you can create any number of constructors with different-2 initialization codes
for different-2 purposes.
StudentData.java

class StudentData{
private int stuID;
private String stuName;
private int stuAge;
StudentData()
{
//Default constructor
stuID = 33;
stuName = "Divesh";
stuAge = 18;
}
StudentData(int num1, String str, int num2)
{
//Parameterized constructor
stuID = num1;
stuName = str;
stuAge = num2;
}
//Getter and setter methods
public int getStuID() {
return stuID;
}
public void setStuID(int stuID) {
this.stuID = stuID;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}

public static void main(String args[])


{
//This object creation would call the default constructor
StudentData myobj = new StudentData();
System.out.println("Student Name is: "+myobj.getStuName());
System.out.println("Student Age is: "+myobj.getStuAge());
System.out.println("Student ID is: "+myobj.getStuID());

/*This object creation would call the parameterized


* constructor StudentData(int, String, int)*/
StudentData myobj2 = new StudentData(36, "ayush dubey", 18);
System.out.println("Student Name is: "+myobj2.getStuName());
System.out.println("Student Age is: "+myobj2.getStuAge());
System.out.println("Student ID is: "+myobj2.getStuID());
}
}
Output:
PS C:\Users\Divesh\AppData\Local\Temp> cd "c:\Users\Divesh\OneDivee\Desktop\Divesh\java\" ; if
($?) {
javac StudentData.java } ; if ($?) { java StudentData }
Student Name is: Divesh
Student Age is: 18
Student ID is: 34
Student Name is: ayush dubey
Student Age is: 18
Student ID is: 36

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy