azarudeen
azarudeen

Reputation: 29

where to use object class in java?

I need to know about object class in java.here are few code I want to know why we use it and where use it

Object obj = arg0.getItemAtPosition(arg2);
String str = obj.toString();

Upvotes: 1

Views: 8894

Answers (4)

eboix
eboix

Reputation: 5133

All "Object"'s inherit from Object, so basically any class in Java is of type Object, too.

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

Because of this, all classes implicitly have a few methods:


Method Summary

protected  Object   clone() 

 boolean    equals(Object obj) 

protected  void finalize() 

 Class  getClass() 

 int    hashCode() 

 void   notify() 

 void   notifyAll() 

 String toString() 

 void   wait() 

 void   wait(long timeout) 

 void   wait(long timeout, int nanos) 

If necessary, you can override these methods to provide your own functionality.

For example, you can override the toString method to return a String that you feel sums up your particular instance of the class. Then, if you pass your Object to something like System.out.println, it will print what the toString method returns.

For more reading:

http://journals.ecs.soton.ac.uk/java/tutorial/java/javaOO/objectclass.html

And here are some nice inheritance tutorials:

http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

http://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html

http://www.cs.utexas.edu/~lavender/courses/tutorial/java-06.pdf

Upvotes: 2

John B
John B

Reputation: 32949

Generally Object should only be used when dealing with a collection of elements of disparate or unknown type. This then usually is followed by instanceof and cast statements. Many APIs return Object when then can provide disparate types and some of this is a holdover from Java 4 and older prior to generics. Whereever possible this should be replaces with generics.

EDIT

Couple basic rules, you should NEVER do the following: Object ob = new Object(); There should never be a reason to instantiate Object in any code. The ONLY time Ojbect should be used is when the type is not know.

An example of how this has changes with generics is Collections.max. In java 4 this returned Object (Java 4 Collections), In java 6 this has a version that returns a generic T (Java 6 Collections).

java 4: public static Object max(Collection coll)

returns Object because at that time Collection was not generic

java 6: public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)

returns T because post Java 5 Collection is generic.

Point being that in the past a lot of APIs returned Object simply because Java did not have generics. Now, in most cases, generics should be used but not all APIs have been updated. Going forward, you should always try to use generics instead of Object whereever possible.

Upvotes: 1

Mohamed_AbdAllah
Mohamed_AbdAllah

Reputation: 5322

The Object class is used when the specific class of a variable is not known. Object class is a parent to many classes (Integer, Sting, ...etc). In the case mentioned above, the getItemAtPosition() class returns an Object instance since a spinner for example can have many different Class types in it, so it returns a general one (Object), then you cast it to String (or other classes depending on the case) when the target type is known in the context.

Upvotes: 1

kosa
kosa

Reputation: 66637

Object is super class of all class in Java. If there is possibility to return more than one type then Object will be returned (or) If API designed to be more generic then Object will be returned.

How to use returned object?

Based on context, you may need to cast (or) do toString() like you did.

Upvotes: 1

Related Questions