Create a class Employee having instance variable name, address, age and salary. Demonstrate use of copy constructor.

 

AIM:  Create a class Employee having instance variable name, address, age and salary. Demonstrate use of copy constructor.

CODE:

import java.util.Scanner;

class Employee{

String name,address;

int age,salary;

//parameterized constructor

Employee(String n,String add, int a,int s){

                        name=n;

                        address=add;

                        age=a;

                        salary=s;

            }

//copy constructor

Employee(Employee e){

                        name = e.name;

                        address = e.address;

                        age = e.age;

                        salary = e.salary;

            }

}

            public class p15{

            public static void main(String args[]){

                       

                        String name,address;

                        int age,salary;

                        Scanner in = new Scanner(System.in);

                        System.out.print("\nEnter employee's name: ");

                        name = in.nextLine();

                        System.out.print("\nEnter employee's address: ");

                        address = in.nextLine();

                        Scanner read = new Scanner(System.in);

                        System.out.print("\nEnter employee's age: ");

                        age = read.nextInt();

                        System.out.print("\nEnter employee's salary: ");

                        salary = read.nextInt();

 

                        emp e = new emp(name,address,age,salary);

                        System.out.println("\n---Employee's details---\n");

                        System.out.println("Name: "+e.name+"\nAddress: "+e.address+"\nAge: "+e.age+"\nSalary: "+e.salary);

 

                        //copy constructor

                        System.out.println("\n---Employee's details in copy constructor---\n");

                        emp e1 = new emp(e);

                        System.out.println(e1.name);

                        System.out.println("Name: "+e1.name+"\nAddress: "+e1.address+"\nAge: "+e1.age+"\nSalary: "+e1.salary);

                       

            }

}

 

 

OUTPUT:



Comments

Popular posts from this blog

Write an application that illustrates method overriding in the same package and different packages. Also demonstrate accessibility rules in inside and outside packages

Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, and Circle. Define one method area()in the abstract class and override this area() in these three subclasses to calculate for specific object.

Write a program that illustrates interface inheritance. Interface P12 inherits from both P1 and P2. Each interface declares one constant and one method. The class Q implements P12. Instantiate Q and invoke each of its methods. Each method displays one of the constants.