Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Protected methods in Object class

Thread view: 
ankur - 22 Nov 2007 19:41 GMT
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
Daniel Pitts - 22 Nov 2007 19:44 GMT
> So I am a Java newbie and have a simple question here:
>
[quoted text clipped - 6 lines]
> 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.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

ankur - 22 Nov 2007 20:19 GMT
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 !
Mike Schilling - 22 Nov 2007 20:19 GMT
> So I am a Java newbie and have a simple question here:
>
[quoted text clipped - 3 lines]
> 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
       }
   }
ankur - 22 Nov 2007 20:42 GMT
On Nov 22, 12:19 pm, "Mike Schilling" <mscottschill...@hotmail.com>
wrote:
> > So I am a Java newbie and have a simple question here:
>
[quoted text clipped - 15 lines]
>         }
>     }

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.
alexander.maystrenko@gmail.com - 22 Nov 2007 21:57 GMT
> On Nov 22, 12:19 pm, "Mike Schilling" <mscottschill...@hotmail.com>
> wrote:
[quoted text clipped - 64 lines]
>
> - 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
Lew - 22 Nov 2007 23:49 GMT
> 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();

Signature

Lew

Ravi - 23 Nov 2007 07:20 GMT
Try this,

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

Ravi

> > public class SubClass extends SuperClass{
>
[quoted text clipped - 58 lines]
> --
> Lew
ankur - 23 Nov 2007 09:04 GMT
> Try this,
>
[quoted text clipped - 65 lines]
> > --
> > Lew

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.
Lew - 23 Nov 2007 09:16 GMT
> 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.

Signature

Lew

ankur - 23 Nov 2007 09:06 GMT
> > public class SubClass extends SuperClass{
> > public class TestClass {
[quoted text clipped - 60 lines]
> --
> Lew

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

Ankur
Lew - 23 Nov 2007 09:18 GMT
> 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.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.