0

clone() and finalize() of the Object class are declared as protected.

Any point in this?

All java classes are inherently descendants of the class Objects-- there's no exception to this.

How would

protected void finalize()

differ in effect from

void finalize()

?

Roam
  • 4,831
  • 9
  • 43
  • 72
  • For your second question, [subclasses would not be able to access the members](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html) if they were declared without an access modifier, ie. they had default visibility. – Sotirios Delimanolis Apr 29 '14 at 01:38
  • Do you mean, how would `protected ...` differ from `public ...` ? – Dawood ibn Kareem Apr 29 '14 at 01:38

1 Answers1

0

It's a little more subtle than you think. If class A and class B both extend class C, and class C has some protected members, then code in class A can't call those protected members via a variable of type B or C, and vice versa. A class can override a protected member and make it public. By making the members protected in class C, you give class B and class A the choice of whether to expose them.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 1
    Erm, they could if they were in the same package. – Brian Roach Apr 29 '14 at 01:41
  • 1
    I think you mean "via an expression whose type is class B", rather than "on an instance of class B". – Dawood ibn Kareem Apr 29 '14 at 01:41
  • @Brian but in general, you can't define new classes in package java.lang. – Ernest Friedman-Hill Apr 29 '14 at 01:43
  • @David Correct, edited. – Ernest Friedman-Hill Apr 29 '14 at 01:45
  • Well ... no, but your reply is more generic than talking about just `Object`. That aside, where it makes a *real* difference is you can't call `.clone()` on an object referenced by a variable typed `Object` regardless of what that instance really is (and this is all ignoring that clone/clonable is terribly broken and should really be avoided, and it's unlikely most people will ever use `finalize()`, at least not correctly :) ). – Brian Roach Apr 29 '14 at 01:47