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());
    }
 
}

Related Posts:

  • The String Constructor:The String Constructor: //Construct one String from other class MakeString{ public Static void main(String[] args){ Char c[]={'d','o','g'}; String s… Read More
  • equals() Versus ==equals() Versus == We should be clear that equals() method and == operator perform two different operations.The equals() method compares the characte… Read More
  • String Concatenation with other data typeString Concatenation with other data type: package stringdemo; public class StringS {     public static void main(String[] args) {  … Read More
  • equals() and equalsIgnoreCase()String Comparision: The String class includes many methods that compares Strings or Substrings within strings. equals() and equalsIgnoreCase() pac… Read More
  • String Concatenation:String Con:catenation Here is a small java program to demonstrate String Concatenation: package stringdemo; public class StringConcat {   &nbs… Read More

0 comments:

Post a Comment