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

Write A Program To Study Default Constructor in Java

The document describes an experiment to write a program to study the default constructor in Java. The program declares a Calc class with integer variables and multiple constructors. The main method creates objects using the different constructors and calls display methods to output the variable values.

Uploaded by

qwerty
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)
30 views4 pages

Write A Program To Study Default Constructor in Java

The document describes an experiment to write a program to study the default constructor in Java. The program declares a Calc class with integer variables and multiple constructors. The main method creates objects using the different constructors and calls display methods to output the variable values.

Uploaded by

qwerty
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/ 4

Experiment 1

Student Name: YANA SRIVASTAVA


UID: 20BCS2279
Branch: CSE
Section/Group : 10 A
Semester: 03
Date of Performance:30-08-2021
Subject Name : JAVA PROGRAMMING LAB
Subject Code: 20CSP-219

PRACTICAL 1.1

1. Aim/Overview of the practical:

Write a program to study Default Constructor in Java.

2. Task to be done/ Which logistics used:

About default constructor default constructor if there is no


constructor available in the class. In such case, Java compiler
provides a default constructor by default.

3. Algorithm/Flowchart (For programming based labs):


1. Start
2. Declare a class named Calc()
3. Inside the class
4. Declare an integer variable len
5. Declare an integer variable br
4. Print the value of len and br by create an object.
5. Call method is used to call the constructor in main function and print
the value .
6. END

4. Steps for experiment/practical/Code:

class Calc{
    int len;
    int br;
    Calc()
    {
    System.out.println("This is a default constructor.");
    }
    Calc(int len, int br)
    {
    this.len= len;
    this.br= br;
    }
    Calc(Calc C)
    {
    len=C.len;
    br=C.br;
    }
    void display()
    {
    System.out.println("Length: "+" " +len);
    System.out.println("Breadth: "+" " +br);
    }
    }
    public class Main {
    public static void main(String[] args)
    {
    //Default Constructor
    Calc C1= new Calc();
    //Parameterized Constructor
    Calc C2= new Calc(2,5);
    //Constructor passed as an argument
    Calc C3= new Calc(C1);
    Calc C4= new Calc(C2);
    //display default constructor
    C3.display();
      //display parameterized constructor
      System.out.println("This is a parameterized constructor.");
    C4.display();
    }
}

5. Observations/Discussions/ Complexity Analysis:

You might also like