Q:

Java program to implement a copy constructor

belongs to collection: Java Class and Object Programs

0

Java program to implement a copy constructor

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to implement constructor overloading is given below. The given program is compiled and executed successfully.

// Java program to implement constructor 
// overloading

class Sample {
  int num1;
  int num2;

  Sample() {
    num1 = 10;
    num2 = 20;
  }

  Sample(int n1, int n2) {
    num1 = n1;
    num2 = n2;
  }

  void printValues() {
    System.out.println("Data members: ");
    System.out.println("Num1: " + num1);
    System.out.println("Num2: " + num2 + "\n");
  }
}

class Main {
  public static void main(String args[]) {
    Sample obj1 = new Sample();
    Sample obj2 = new Sample(30, 40);

    obj1.printValues();
    obj2.printValues();
  }
}

Output:

Data members: 
Num1: 10
Num2: 20

Data members: 
Num1: 30
Num2: 40

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Class and Object Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to implement constructor chaining... >>
<< Java program to implement constructor overloading...