0

As for java architecture is concern "to keep things as much private as possible".Thats why finalize method is protected in object class.

protected void finalize() throws Throwable { }

Why is the finalize() method in java.lang.Object “protected”?please have a look on Mehrdad Afshari answer.

then why many other method like equals(),toString(),wait(),notify() are there public.they can be protected and they still retain their existing behaviour.where protected cause issues?``

Community
  • 1
  • 1
TheCurious
  • 593
  • 1
  • 4
  • 29
  • since object class is parent of every class .so protected will available to rest of world like public. – TheCurious Apr 24 '15 at 07:18
  • Please look up how inheritance works, it is not "available to rest of world like public". – Smutje Apr 24 '15 at 07:21
  • 1
    @Smutje protected of object will always treat same as public of object class,since object class is parent of each and every class. – TheCurious Apr 24 '15 at 07:33
  • 1
    No, you can't call `protected` methods of other objects than your own and that's what `protected` is all about. – Smutje Apr 24 '15 at 07:34
  • @Smutje but we can make a call on of protected method of extended class. – TheCurious Aug 10 '17 at 04:52
  • You can only call your *own* inherited protected methods, *not* the protected methods of other objects. `finalize` may be part of `Object` but that does not mean objects of class `A` can call `finalize` on an object of class `B`. – Smutje Feb 26 '18 at 08:40

3 Answers3

3

Try calling finalize:

new Object().finalize();

It won't compile: subclasses only have access to protected methods on other objects of the same type.

If equals, toString, wait and notify were protected, we could not access them freely.

In JLS terms...

Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.

In addition, if Id denotes an instance field or instance method, then:

  • If the access is by a field access expression E.Id, or a method invocation expression E.Id(...), or a method reference expression E::Id, where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.

In simpler terms, protected members are accessible on...

  • ...itself:

    package ex1;
    public class Example1 {
        protected void m() {}
    }
    
    package ex2;
    import ex1.Example1;
    public class Example2 extends Example1 {{
        m(); // m is accessible
    }}
    
  • ...other instances of the same type (or a subclass of it):

    package ex3;
    import ex1.Example1;
    public class Example3 extends Example1 {{
        new Example3().m(); // m is accessible
        new Example1().m(); // m is NOT accessible
    }}
    
Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • hardly I know some one does `new Object().equals`,`new Object().toString()`, `new Object().wait()` and `new Object().notify()`,even this doesn't serves any purpose,we always call these method over sub class object only,same as `finalize()`.Not clarifying the fact,can you pls explain? – TheCurious Aug 04 '17 at 12:34
  • @TheCurious `new Object().finalize()` is just a short way to illustrate the point. `Object o = new Object(); o.finalize();` has the same result, which is a compilation error. – Radiodef Aug 04 '17 at 13:34
  • have you ever seen some where `new Object().toString()` or `o.toString()`.we always did anyClass.toString().What is the advantage of public over proetected? – TheCurious Aug 08 '17 at 10:41
  • I don't understand what you're trying to ask. Here's another example of `protected` methods not being accessible to objects of different types: http://ideone.com/NNpc3I. Here are examples of code which wouldn't compile if the methods of `Object` were `protected`: [1](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/util/Objects.java#Objects.equals%28java.lang.Object%2Cjava.lang.Object%29), [2](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/util/ArrayList.java#ArrayList.indexOf%28java.lang.Object%29). – Radiodef Aug 08 '17 at 13:46
  • Ok goit it,you concern is if you designed code like this which takes `Object` as argument `ex-(Object o)`no matter all inherited class have its own implementation of `toSrting` ,`o.toString()` would not compile. thats why this is not `protected` or even `abstract protected` – TheCurious Aug 09 '17 at 12:43
2

finalize is not intended to be called from outside, because it shall contain code to clean up an object whilst equals(), toString(), wait(), notify() are intended to be called from another object to ensure equality, a string representation or other functionality.

Smutje
  • 17,733
  • 4
  • 24
  • 41
2

These methods have to be called by classes that have no relation to the classes for which they are called.

For example, HashMap<SomeClass> won't work if it can't call equals() and hashCode() of SomeClass, so they must be public.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Is your concern, if you designed code like this which takes `Object` as argument ex-(Object o)no matter all inherited class have its own implementation of `toSrting` ,`o.toString()` would not compile. thats why this is not `protected` or even `abstract protected`?So can it not handled using generics,can be not call `t.toString()` where `T `could be wildcard `?`. – TheCurious Aug 10 '17 at 04:48
  • @TheCurious I don't see how generics would help you in this case (but even if they could, they were introduced in Java 5, long after the `Object` class was written). Methods like `equals` and `hashCode` are intended to be called by objects that may belong to different packages, so they must be `public` in order to allow them to be called by the code of any class. – Eran Aug 10 '17 at 09:00
  • protected method can also be called from outside the packages,if any class extends it, and Object class is inherit by all the classes over the world.Only thing I am thinking if somewhere we deigned generalized code like this, which accept parameter as `Object` we cant access protected method using reference of this `Object`. Please Correct me if I am wrong. – TheCurious Aug 10 '17 at 09:12