• Post Reply Bookmark Topic Watch Topic
  • New Topic

Methods in Object class  RSS feed

 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

We know that all classes in Java extend the Object class. But methods in Object class are declared as public. What is the reason for this?

I think if they were declared as protected, then also there wont have been any issue. So, what is the reason behind making them as public?

Thanks.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav G Garg wrote:
We know that all classes in Java extend the Object class. But methods in Object class are declared as public. What is the reason for this?

I think if they were declared as protected, then also there wont have been any issue. So, what is the reason behind making them as public?



What "issue" are you referring to?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's look at for example the toString() method, which is one of the public methods in class java.lang.Object.

What would happen if it were protected instead of public?

Then you could not call toString() anymore on any random object. Remember what 'protected' means: it means the method is only accessible from the class itself, its subclasses or classes in the same package. But not from classes that are unrelated to it in the class hierarchy and that are in another package.

Suppose, for example, that you would have a class named "Example" and that you wanted to call toString() on a java.util.Date object:

This program would not compile anymore if toString() were protected in class Object, because class Example is not related to class Date - it has no access to the toString() method of the Date object.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav G Garg wrote:I think if they were declared as protected, then also there wont have been any issue. So, what is the reason behind making them as public?

I think Jesper's explained most of it to you; and it's that "random" part that's important.

You could certainly define subclasses that upgrade those methods to public and then, as long as you refer to the subclass by its type, you could call those methods.

But if you define a method that takes an Object as a parameter, then a client is allowed to pass any reference type to it - including an actual Object, or an anonymous class derived from one - and in those cases you would NOT be allowed to call its methods. And since the definition of the parameter is Object, the compiler can't possibly know what the actual type is, and therefore can't allow you call any protected methods.

Basically, those methods are part of ANY object's public API, so anyone should be able to call them any time, any place.

Hope it makes sense.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!