All non-private member methods are inherited. Protected simply means
that you have to be at or below the class that declares it to access it.
The only thing that overriding does for it is to create a public method
with the same signature that delegates to the protected method.

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
On Nov 22, 11:44 am, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
> > So I am a Java newbie and have a simple question here:
>
[quoted text clipped - 15 lines]
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
I am still not able to understand the issue with these class
declarations:
I have following classes declared in the same package:
public class Sample implements Cloneable {
private int j;
public Sample(int h)
{
j = h;
}
public void display()
{
System.out.printf("The value of private variable is %d\n", j);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class TestSample{
/**
* @param args
*/
public static void main(String[] args) {
Sample obj = new Sample(78);
obj.clone();
}
}
I am not able to understand why can't obj refer to clone() method.
Afterall obj is of type Sample which extends Object class by default.
Roedy Green - 23 Nov 2007 12:06 GMT
On Thu, 22 Nov 2007 12:19:19 -0800 (PST), ankur
<ankur.a.agarwal@gmail.com> wrote, quoted or indirectly quoted someone
who said :
>I am not able to understand why can't obj refer to clone() method.
>Afterall obj is of type Sample which extends Object class by default.
see http://mindprod.com/jgloss/scope.html
http://mindprod.com/jgloss/protected.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Ravi - 23 Nov 2007 12:24 GMT
Ok, may be you are missing the trivial point here.
Object is in java.lang package.
default package isn't java.lang.
Does this make things clear?
On Nov 23, 5:06 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Thu, 22 Nov 2007 12:19:19 -0800 (PST), ankur
> <ankur.a.agar...@gmail.com> wrote, quoted or indirectly quoted someone
[quoted text clipped - 8 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
Thomas Fritsch - 23 Nov 2007 12:41 GMT
> I am still not able to understand the issue with these class
> declarations:
[quoted text clipped - 12 lines]
> System.out.printf("The value of private variable is %d\n", j);
> }
public Object clone()
{
try
{
return super.clone(); // call Object's clone() method
}
catch(CloneNotSupportedException e)
{
// cannot happen because Sample implements Cloneable
throw new InternalError();
}
}
> }
>
[quoted text clipped - 12 lines]
> I am not able to understand why can't obj refer to clone() method.
> Afterall obj is of type Sample which extends Object class by default.
Because Object's clone() method is protected, it is callable only from
subclasses (from class Sample in your case), but not from outside
Object's package java.lang (i.e. not from your class TestSample).
To solve your issue you have to insert a public clone method into your
class Sample. See above.

Signature
Thomas
Lew - 23 Nov 2007 16:28 GMT
@Override
> public Object clone()
> {
[quoted text clipped - 9 lines]
> }
>> }

Signature
Lew
ankur - 23 Nov 2007 19:38 GMT
> @Override
>
[quoted text clipped - 14 lines]
> --
> Lew
Thanks everybody for help ! I understood now. Object class is not the
same package as other classes that extend it. Also protected members
have package access and are also inherited by subclasses where they
can be referenced using super. Cool !!!
Thanks for your help everybody !