Advertisement
apl-mhd

OOP copyConstructor

May 30th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.51 KB | None | 0 0
  1. package com.company;
  2.  
  3. public class Main {
  4.  
  5.  
  6.     int id;
  7.     String name;
  8.  
  9.     Main(int i, String nm){
  10.  
  11.         id =i;
  12.         name = nm;
  13.     }
  14.  
  15.     Main(Main copy){
  16.  
  17.         id = copy.id;
  18.         name = copy.name;
  19.  
  20.     }
  21.  
  22.     void display(){
  23.  
  24.         System.out.println("ID :"+id+" Name: "+name);
  25.     }
  26.  
  27.  
  28.  
  29.     public static void main(String[] args) {
  30.  
  31.         Main one = new Main(1,"orko");
  32.         Main two = new Main(one);
  33.  
  34.         one.display();
  35.         two.display();
  36.  
  37.  
  38.  
  39.  
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement