2

One thing I have learnt is private in java doesn't really mean same thing as that in C++. private in java is class-based, not object-based. i.e I can access another objects private member directly using "object dot notation" provided that I do so within the class of that object.

However, protected is not so clear. We will have to consider two packages here : pack1 and pack2
We declare classes Alpha and Beta in pack1 package
and declare AlphaSub which extends Alpha from pack1 , and Gamma which also extends from Alpha in pack2 package. !

enter image description here

Here is the class code, I have only included classes relevant to the issue here : Alpha, AlphaSub and Gamma

package pack1;

public class Alpha{
  private int alphaPrivate;
  public int alphaPublic;
  protected int alphaProtected;
  int alphaPP;

  protected int alphaProtected(){
    return 1;
  }
  private int alphaPrivate(){
    return 1;
  }
}

package pack2;

import pack1.Alpha;
public class AlphaSub extends Alpha{
    int alphasubPP;
    private int alphasubPrivate;
    protected int alphasubProtected;
    public int alphasubPublic;

    public void meth() throws CloneNotSupportedException{
        new AlphaSub().alphaProtected(); //OK
        new Gamma().alphaProtected(); /*COMPILE ERROR. */

    }
}

So apparently even though both AlphaSub and Gamma inherits alphaProtected() from Alpha , one cannot invoke Gamma's inherited alphaProtected() from AlphaSub .. If this is the case that protected method of a class can only be called from within that class, wouldn't invocation of clone [inherited by every class from Object class] from another class be impossible ??

Someone can please clarify ?

Sarabjeet
  • 264
  • 2
  • 17
  • 2
    possible duplicate of [In Java, what's the difference between public, default, protected, and private?](http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private) – Andy Turner Feb 17 '15 at 15:58

1 Answers1

1

What you experienced is covered in JLS 6.6.2.1.:

6.6.2.1. Access to a protected Member

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 qualified name Q.Id or a method reference expression Q :: Id (§15.13), where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.

  • 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 (§15.8), then the access is permitted if and only if the type of E is S or a subclass of S.

  • If the access is by a method reference expression T :: Id, where T is a ReferenceType, then the access is permitted if and only if the type T is S or a subclass of S.

And you are correct, if you replace new Gamma().alphaProtected(); with new Gamma().clone();, you'll get the same compilation error.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • yet we should be able to invoke `new Gamma().clone` from outside `Gamma` class's body, no ? otherwise what significant purpose will the `clone` method serve for Gamma class ? I think `clone` method would have to be redefined in `Gamma` for it to be invoked from outside `Gamma`, no ?? please clarify\ – Sarabjeet Feb 17 '15 at 16:20
  • oh yes , it actually does. Just overrode the protected method in Gamma and it works ! funny how there's so much more to things than whats simply stated in tutorial docs – Sarabjeet Feb 17 '15 at 16:29