If everything is an object , Cant we declare every object as<br /> <br /> Object obj = new String();

Search Interview Questions

 3027 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text


Java - Interview Questions and Answers

 Q1. If everything is an object , Cant we declare every object as

Object obj = new String();
Core Java2016-12-02 14:45:05

Ans. Yes, we can do that. Compiler wont complain. But using object reference we can only access methods which have been defined for object class i.e clone(), equals(), hashCode(), toString() etc.

We cannot access methods defined in String class or in any class in hierarchy between String and Object.

For example - we cannot do obj.append("abc") as it will now give compile time error.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     object initialization  object declaration   runtime polymorphism

Related Questions

Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner

comments powered by Disqus

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   

Questions Asked: