0% found this document useful (0 votes)
3 views4 pages

Java Practical No 10

The document contains a Java program that defines a class 'Stud' with various constructors, including a default, parameterized, overloaded, and copy constructor. It also includes a display method to show the details of the student and a finalize method to simulate a destructor. The main class creates instances of 'Stud' using different constructors and demonstrates their functionality.

Uploaded by

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

Java Practical No 10

The document contains a Java program that defines a class 'Stud' with various constructors, including a default, parameterized, overloaded, and copy constructor. It also includes a display method to show the details of the student and a finalize method to simulate a destructor. The main class creates instances of 'Stud' using different constructors and demonstrates their functionality.

Uploaded by

sunshine004408
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PRACTICAL 10

class Stud {
int rollNo;
String name;
float marks;
// Default Constructor
Stud() {
rollNo = 10;
name = "jjjjj";
marks = 1.20;
[Link]("Default Constructor Called");
}

// Parameterized Constructor
Stud(int r, String n, float m) {
rollNo = r;
name = n;
marks = m;
[Link]("Parameterized Constructor Called");
}

// Overloaded Constructor
Stud(int r, String n) {
rollNo = r;
name = n;
marks = 0.0f;
[Link]("Overloaded Constructor Called");
}
// Copy Constructor (user-defined)
Stud(Stud s) {
rollNo = [Link];
name = [Link];
marks = [Link];
[Link]("Copy Constructor Called");
}

// Display Method
void display() {
[Link]("Roll No: " + rollNo);
[Link]("Name : " + name);
[Link]("Marks : " + marks);
[Link]("-----------------------");
}

// Destructor-like method
protected void finalize() {
[Link]("Destructor Called for " + name);
}
}

public class xyz {


public static void main(String[] args) {

// Default constructor
Stud s1 = new Stud();
[Link]();
// Parameterized constructor
Stud s2 = new Stud(1, "Amit", 85.5f);
[Link]();

// Overloaded constructor
Stud s3 = new Stud(2, "Neha");
[Link]();

// Copy constructor
Stud s4 = new Stud(s2);
[Link]();

// Hint JVM for garbage collection


s1 = null;
[Link]();
}
}
OUTPUT :

Default Constructor Called


Roll No: 10
Name : jjjjj
Marks : 1.2
-----------------------
Parameterized Constructor Called
Roll No: 1
Name : Amit
Marks : 85.5
-----------------------
Overloaded Constructor Called
Roll No: 2
Name : Neha
Marks : 0.0
-----------------------
Copy Constructor Called
Roll No: 1
Name : Amit
Marks : 85.5
-----------------------
Destructor Called for jjjjj

You might also like