Constructor

 avatar
zoro
java
11 days ago
532 B
0
Indexable
Never
class Demo {

  int a;

  public Demo() { //default constructor
    System.out.println("Default Constructor");
  }

  public Demo(int val) { //constructor overloading
    a = val;
    System.out.println("Overloaded Constructor, Value: " + a);
  }

  public Demo(Demo copy) {
    a = copy.a;
    System.out.println("Copy Constructor, Value: " + a);
  }
}

public class Pr1a {

  public static void main(String args[]) {
    Demo d = new Demo();
    Demo d1 = new Demo(5);
    Demo d2 = new Demo(d1);
  }
}
Leave a Comment