66 Method Overriding in Java

Overriding toString() method.

Code:

package com.ca.atharva;

class Student
{
int sid = 101;
String sname = "ram";

public String toString()
{
return sid+"..."+sname;
}
public static void main( String args[] )
{
Student s = new Student();
System.out.println(" S: "+s); // toString()
// packagename.classname@memory in Hexa Decimal Format
}
}


Output:


Previous
Next Post »