• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Query about Object class

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to java . I have two doubts in it

1) In object class there are few methods that are declared as native and are not being implemented .

      protected native Object clone() throws CloneNotSupportedException;
    public native int hashCode();

    public final native void wait(long timeout) throws InterruptedException;
    public final native void notify();


and many more ...................

But then too Object class is not declared as abstract .

2)     Object obj = new Object();
    obj.clone() // compiler error ( method is not visible )

even if clone is declared as protected .

Please make me clear on this .

Thanks in advance.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jawatch java wrote:I am new to java . I have two doubts in it

1) In object class there are few methods that are declared as native and are not being implemented .
But then too Object class is not declared as abstract .



A class doesn't need to be declared abstract if it has native methods, only if it has abstract methods.

2)     Object obj = new Object();
    obj.clone() // compiler error ( method is not visible )

even if clone is declared as protected .



You can't just call clone on anything from a subclass. The rules boil down to, roughly, you can only call it using "super" or "this".
 
subuhi nigar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply.
I m sorry , still not clear on 2nd doubt.
My doubt is why clone() is not visible there ? irrespective of how to use clone().
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jawatch java wrote:Thanks for reply.
I m sorry , still not clear on 2nd doubt.
My doubt is why clone() is not visible there ?






The part of the rules of protected access that allow a subclass to access the member only apply when accessing a member of the current object. We can't use them to access a protected member of any old object just because we happen to be a subclass of that object's class.
 
subuhi nigar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran this program ( modified little by adding s.o.p )
It ran fine and printed output foo 4 times.


Compiler didnt say it's not allowed.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try out following:


 
subuhi nigar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes ! i tried and this time compiler showed error .
How it happened !!?? I just changed the packages .
It seems protected keyword is visible to inherited class objects irrespective of package and not to the base class object.
To base class object it will act as default access specifiers.
I guess now i understood.

Thanks much Jeff and Gaurangkumar for the example .
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jawatch java wrote:How it happened !!?? I just changed the packages .
It seems protected keyword is visible to inherited class objects irrespective of package and not to the base class object...


Don't guess, find out. Reading the tutorials (or the JLS) really is the only way to be sure that you've got it right.

Winston
reply
    Bookmark Topic Watch Topic
  • New Topic