0

Why can we access the clone() method using super. notation but cant access it by creating new object of type Object. For example if I do something like

Object obj = new Object();

then (using Eclipse) I cannot view the clone() method using the dot operator, which shows that obj is not able to view protected members of Object class (only public methods are visible).

Now if I were to use the super keyword i.e use super. I am able to view protected methods as well.

So my question is that in a class which does not explicitly inherit (extend) other class -- i know that it gives an implicit call to the Object class constructor, i.e. the Object class is its superclass-- why can super keyword access the protected members of Object class, but same is not achieved by creating instance of Object class (only the public members are visible to the instance of Object class)?

Here is clearer code (although it makes no sense but the first part complies and other doesn't):

public class temp {

    public temp() {
        // TODO Auto-generated constructor stub
        TestBikes t1 = new  TestBikes();
        Object ob1 = new Object();
        try {
            t1 = (TestBikes)super.clone(); //SUPER KEYWORD IS ABLE TO ACCESS
                                                   //PROTECTED MEMBERS OF Object 
                                                   //CLASS   
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            t1 = (TestBikes)ob1.clone();  // HERE IS THE ERROR SAYING clone()
                                                  // FROM Object CLASS IS NOT VISIBLE
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 4
    This is confusing, can you post sample code? – fge Jan 02 '13 at 17:05
  • 2
    I suggest you study the difference between private, protected and public – asgoth Jan 02 '13 at 17:07
  • The cause of that behavior are [modifiers](http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private) – Damian Leszczyński - Vash Jan 02 '13 at 17:10
  • i did study the access modifiers from the oracle tutorials but according to them "protected" type allows access to class,package and subclass. but since Object class is superclass to all classes, why an instance of Object class is unable to view its own protected members? – Kraken Infinity Jan 02 '13 at 17:41

1 Answers1

4

protected means only accessible by a sub-class (or a class in the same package) So you can access it in a sub-class using this or super or nothing, but not another instance.

Your example compiles but it won't run.

    try {
        t1 = (TestBikes)super.clone(); // creates a clone of temp, not a TestBikes
        t1 = (TestBikes)this.clone(); // does the same thing  
        t1 = (TestBikes)clone(); // does the same thing  

    } catch (CloneNotSupportedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

BTW: I know this is just an example but cloning an object in it's constructor is a bad idea.


It has to be protected so you can access it in subclasses which implement Cloneable. However, not all objects are cloneable so it is not generally accessible.

Using super can access protected methods. Using a reference to make a method requires the method be accessible e.g. public or package local and in the same package.

AFAIK, It is generally accepted that the clone()/Cloneable API is broken and should be avoided (except for primitive arrays) e.g. The Cloneable interface doesn't have a Object clone(); method to make it public and it returns Object which have to cast.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    While the OP is trying to access and use clone I think the question is really trying to understand why he can't access protected members of a class when calling new when instantiating the object within a subclass of a derived type. The question seems to asks 'whats the difference between new and super' though it is a little unclear. – Paul Sullivan Jan 02 '13 at 17:23
  • it all boils down to why "super" is able to access the protected methods of the OBJECT class but the same is not applicable for an object created from OBJECT class. Sorry for being unclear in question( i tried my best :P) – Kraken Infinity Jan 02 '13 at 17:36
  • `protected` means only accessible by a sub-class (or a class in the same package) So you can access it in a sub-class using `this` or `super` or nothing, but not another instance. – Peter Lawrey Jan 02 '13 at 17:41
  • @PeterLawrey - You should put your last comment as the answer. Succinct and correct. – Paul Sullivan Jan 02 '13 at 17:43
  • @KrakenInfinity Added an example. – Peter Lawrey Jan 02 '13 at 17:44
  • 1
    @PeterLawrey - spot on and you can't explain any better. If the OP still doesn't understand then it's through a lack of understanding of OOP and encapsulation/access modifiers. – Paul Sullivan Jan 02 '13 at 17:48
  • 1
    "not another instance" is slightly misleading. You *can* call protected methods on other instances of the type of `this`. I.e. a method in the class `temp` *can* call a protected method on another instance of `temp`. It just can't call protected methods on instances of *other classes*. – Joachim Sauer Jan 02 '13 at 18:02
  • @JoachimSauer: The only "extra" access that `protected` offers which `private` does not is for base-type members of `this`. One can access `protected` members of other instances on the same terms as one could access their `private` members. – supercat Jan 07 '13 at 19:30