Why is the Object class super class of all the classes?

Questions by cindy7alex   answers by cindy7alex

Showing Answers 1 - 2 of 2 Answers

k.v.n.r.muralil krishna

  • Apr 3rd, 2006
 

in object class it contain some methods like hashCode(), toString() methods. this two methods is used to give some information. for example in my project devlepoment i use several classes at some point time i get confused what sort of information i used in my class.so i write my class information(i.e what methods,instance variable etc) in toString() method. when i use this method if i want print my object,instead giving hashcode,its give class information. for that purpose i write object class.

ex: class a{

     String name;

  public String toString(){

System.out.println(name);

}

}

class mainone{

public static void main(String args[])throws Exception{

a aa=new a();

System.out.println(a);//hear we get name

}

}

  Was this answer useful?  Yes

jeethu

  • Apr 4th, 2006
 

1class a{

2   String name;

3 public String toString(){

4 System.out.println(name);

5 }

6 }

7 class mainone{

8 public static void main(String args[])throws Exception{

9 a aa=new a();

10 System.out.println(a);//hear we get name

11 }

12 }

this program has two compilation errors.

1.missing return statement(line 4)

2.cannot find variable a(line 10)

correct code is:

class a{

     String name="meera";

  public String toString(){

return(name);

}

}

class manone{

public static void main(String args[])throws Exception{

a aa=new a();

System.out.println(aa);//hear we get name

}

}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions