Sunday, December 13, 2015

Java program to Overide toString Method:

Java program to Overide toString Method:


package overidetostringmethod;


public class Employee {
int empid;
String empName;
String empAddress;

 Employee() {
         this.empid=0;
 this.empName="default";
 this.empAddress="default";

    }
    
    public Employee(int empid,String empName,String empAddress){
    this.empid=empid;
    this.empName=empName;
    this.empAddress=empAddress;
    
    
    }

   
    
    public String toString(){
    String str= this.empid + this.empName +this.empAddress;
    return str;
    }

}
*************************************

package overidetostringmethod;
import java.util.Scanner;

public class MyClass {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        
        
        Employee obj1= new Employee();
        System.out.println("Enter id");
        int id=input.nextInt();
        
        System.out.println("Enter Name");
        String name=input.next();
        
        System.out.println("Enter Address");
        String Address=input.next();
        
        Employee obj2= new Employee(id,name,Address);
        System.out.println(obj1.toString());
    }
 
}

0 comments:

Post a Comment