Protected methods in Object class

A

ankur

So I am a Java newbie and have a simple question here:

protected methods of a superclass are inheritable by the subclass. So
how come the protected methods of Object class namely clone and
finalize not inherited by every class by default ie why is overriding
for these methods necessary in the class which wants to use these
methods and not accessed directly.

Thanks,
Ankur
 
D

Daniel Pitts

ankur said:
So I am a Java newbie and have a simple question here:

protected methods of a superclass are inheritable by the subclass. So
how come the protected methods of Object class namely clone and
finalize not inherited by every class by default ie why is overriding
for these methods necessary in the class which wants to use these
methods and not accessed directly.

Thanks,
Ankur
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.
 
A

ankur

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.

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.
 
M

Mike Schilling

ankur said:
So I am a Java newbie and have a simple question here:

protected methods of a superclass are inheritable by the subclass. So
how come the protected methods of Object class namely clone and
finalize not inherited by every class by default ie why is overriding
for these methods necessary in the class which wants to use these
methods and not accessed directly.

Partly, because you're misunderstanding what "protected" means. It's
available in a subclass only to object whose type is that subclass.

class Class1
{
void method1(Object o)
{
Class1 dup = (Class1)this.clone(); // good
Object dup2 = o.clone() // will not compile
}
}
 
A

ankur

Partly, because you're misunderstanding what "protected" means. It's
available in a subclass only to object whose type is that subclass.

class Class1
{
void method1(Object o)
{
Class1 dup = (Class1)this.clone(); // good
Object dup2 = o.clone() // will not compile
}
}

Mark,

However in the examples below protected method of Superclass is
available in Testclass to object of type Subclass.


public class SuperClass {
private int j;
protected void messageinsuper()
{
System.out.printf("This is superclass\n");
}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


public class SubClass extends SuperClass{

private int g;
public void messageinsub()

{
System.out.printf("This is a subclass\n");
}

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


public class TestClass {


public static void main(String[] args) {

SubClass obj = new SubClass();
obj.messageinsub();
obj.messageinsuper();
}

}

Earlier also I was doing the same thing. Sample extends Object and
TestSample declares an object of type Sample and tries to access
protected method Clone.
 
A

alexander.maystrenko

Partly, because you're misunderstanding what "protected" means. It's
available in a subclass only to object whose type is that subclass.
class Class1
{
void method1(Object o)
{
Class1 dup = (Class1)this.clone(); // good
Object dup2 = o.clone() // will not compile
}
}

Mark,

However in the examples below protected method of Superclass is
available in Testclass to object of type Subclass.

public class SuperClass {
private int j;
protected void messageinsuper()
{
System.out.printf("This is superclass\n");
}

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public class SubClass extends SuperClass{

private int g;
public void messageinsub()

{
System.out.printf("This is a subclass\n");
}

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public class TestClass {

public static void main(String[] args) {

SubClass obj = new SubClass();
obj.messageinsub();
obj.messageinsuper();
}

}

Earlier also I was doing the same thing. Sample extends Object and
TestSample declares an object of type Sample and tries to access
protected method Clone.- Hide quoted text -

- Show quoted text -

Hey, just take a look on the java api
http://java.sun.com/j2se/1.5.0/docs/api/
protected Object clone() -- so it's not public!

mad0master
 
L

Lew

ankur said:
public class SubClass extends SuperClass{
public class TestClass {
public static void main(String[] args) {

Please do not use TAB characters in Usenet posts.
SubClass obj = new SubClass();
obj.messageinsub(); // should capitalize each word part: messageInSub()
obj.messageinsuper();

You're in the same package.

A class can access protected members of another class in its same package.

However, when I try to use clone() I get an error:

===
package testit;
public class Super
{
protected void message()
{
System.out.println( "Super.message()" );
}
}

===
package testit;
public class Sub extends Super
{
public void tell()
{
message();
System.out.println( "Sub.tell()" );
}
}

===
package testit;
public class Checkit
{
public static void main(String[] args)
{
Sub sub = new Sub();
sub.tell();
sub.message();
Sub s2 = (Sub) sub.clone(); // line 9
}
}

===
Compilation of Checkit:
projects/testit/src/testit/Checkit.java:9: clone() has protected access in
java.lang.Object
Sub s2 = (Sub) sub.clone();
 
R

Ravi

Try this,

In order to call the protected method, u should be in the same package
as the base class. Not subclass.

Ravi
ankur said:
public class SubClass extends SuperClass{
public class TestClass {
public static void main(String[] args) {

Please do not use TAB characters in Usenet posts.
SubClass obj = new SubClass();
obj.messageinsub(); // should capitalize each word part: messageInSub()
obj.messageinsuper();

You're in the same package.

A class can access protected members of another class in its same package.

However, when I try to use clone() I get an error:

===
package testit;
public class Super
{
protected void message()
{
System.out.println( "Super.message()" );
}
}

===
package testit;
public class Sub extends Super
{
public void tell()
{
message();
System.out.println( "Sub.tell()" );
}
}

===
package testit;
public class Checkit
{
public static void main(String[] args)
{
Sub sub = new Sub();
sub.tell();
sub.message();
Sub s2 = (Sub) sub.clone(); // line 9
}
}

===
Compilation of Checkit:
projects/testit/src/testit/Checkit.java:9: clone() has protected access in
java.lang.Object
Sub s2 = (Sub) sub.clone();
 
A

ankur

Try this,

In order to call the protected method, u should be in the same package
as the base class. Not subclass.

Ravi
ankur said:
public class SubClass extends SuperClass{
public class TestClass {
public static void main(String[] args) {
Please do not use TAB characters in Usenet posts.
You're in the same package.

A class can access protected members of another class in its same package.
However, when I try to use clone() I get an error:
===
package testit;
public class Super
{
protected void message()
{
System.out.println( "Super.message()" );
}
}
===
package testit;
public class Sub extends Super
{
public void tell()
{
message();
System.out.println( "Sub.tell()" );
}
}
===
package testit;
public class Checkit
{
public static void main(String[] args)
{
Sub sub = new Sub();
sub.tell();
sub.message();
Sub s2 = (Sub) sub.clone(); // line 9
}
}
===
Compilation of Checkit:
projects/testit/src/testit/Checkit.java:9: clone() has protected access in
java.lang.Object
Sub s2 = (Sub) sub.clone();

Ravi,

I am using eclipse with these examples:
All three classes are in their own java files and in default package.

public class SuperClass {
private int j;
protected void messageinsuper()
{
System.out.printf("This is superclass\n");
}

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public class SubClass extends SuperClass{

private int g;
public void messageinsub()

{
System.out.printf("This is a subclass\n");
}

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public class TestClass {

public static void main(String[] args) {

SubClass obj = new SubClass();
obj.messageinsub();
obj.messageinsuper();
}

}

This works perfectly ie obj can call both messageinsub() and
messageinsuper()

Now I don't understand why this does not work:

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();

}

}

After all Sample extends object by default and just like the earlier
example I am trying to access protected method of superclass Object
which is clone() in a TestSample class. All are in the default package
in eclipse.
 
A

ankur

ankur said:
public class SubClass extends SuperClass{
public class TestClass {
public static void main(String[] args) {

Please do not use TAB characters in Usenet posts.
SubClass obj = new SubClass();
obj.messageinsub(); // should capitalize each word part: messageInSub()
obj.messageinsuper();

You're in the same package.

A class can access protected members of another class in its same package.

However, when I try to use clone() I get an error:

===
package testit;
public class Super
{
protected void message()
{
System.out.println( "Super.message()" );
}

}

===
package testit;
public class Sub extends Super
{
public void tell()
{
message();
System.out.println( "Sub.tell()" );
}

}

===
package testit;
public class Checkit
{
public static void main(String[] args)
{
Sub sub = new Sub();
sub.tell();
sub.message();
Sub s2 = (Sub) sub.clone(); // line 9
}

}

===
Compilation of Checkit:
projects/testit/src/testit/Checkit.java:9: clone() has protected access in
java.lang.Object
Sub s2 = (Sub) sub.clone();

Lew,
Agreed clone() has protected access but so does method message() which
belongs to superclass. Doesn't it ?

Ankur
 
L

Lew

Ravi said:
Try this,

In order to call the protected method, u [sic] should be in the same package
as the base class.

Uhh, yeah, that was my point.
Not subclass.

Except that you *can* call a protected method from a subclass; that's the
purpose of 'protected'.

Please do not top-post.
 
L

Lew

ankur said:
Agreed clone() has protected access but so does method message() which
belongs to superclass. Doesn't it ?

Yes, and as I pointed out, Sub, Super and Checkit are also in the same
package. Object is not.
 
R

Ravi

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?
 
T

Thomas Fritsch

ankur said:
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 Object clone()
{
try
{
return super.clone(); // call Object's clone() method
}
catch(CloneNotSupportedException e)
{
// cannot happen because Sample implements Cloneable
throw new InternalError();
}
}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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.
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.
 
L

Lew

@Override
public Object clone()
{
try
{
return super.clone(); // call Object's clone() method
}
catch(CloneNotSupportedException e)
{
// cannot happen because Sample implements Cloneable
throw new InternalError();
}
}
 
A

ankur

Thomas Fritsch wrote:

@Override

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 !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top