• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Object class in provided by java has some native methods with protected access specifier. Why the author of Object class would have thought of protected access is to such native methods?

Hello
Question is in the title bar
0
mohet01
Asked:
mohet01
  • 5
  • 5
1 Solution
 
for_yanCommented:
0
 
for_yanCommented:

How do you judge which of the java.lang.Object methods are native?

It looks like in all recent APIs, starting say from Java 1.4 they no longer
indicate directly which of them are native.

And also in the recent API, say in Java 6,
 I see there are only two protected methods of java.lang.Object - protected Object clone() and
protected void  finalize(). Are those native?

In general, I think you pose a very interestng question -
why do they make these methods protected, especially considering
that all classes inherit from java.lang.Object, and
on the surface all should be able to access protected methods
of Object.

Judging by discussions on Internet and even
some discussions in the books it is not totally clear to the majority
of people.

I can't say it is clear for me, but I can venture a guess.
Object was mainly designed for subclassing.
Once you create a new class which is a subclass of Object
you will be able to use its clone method only in the
process of implementation  of this subclass.
Because of the protected nature of say clone() method
you'll not be able to use it say in such
context

MyClass mm = new MyClass(...);

MyClass mm1 = mm.clone();
Protected access would prevent you from doing it.

If you really need to use it in  this way somewhere
in your operations with your custm class
(if you need to clone your custom objects)
then for your MyClass object,
you'll have to override the clone()
method of Object with less restricted
scope (say, with public access).

So in this way they in fact force you to implement
this method when subclassing Object.

In fact this clone() method of the Object acts more
like an abstract method, it actually requires
implementation. The big difference though
with abstract method is that in case of abstract
method you need to implement it when
you create the class no matter what, other wise your
class will become abstract. In this case,
you may choose not to implement it
if you actually do not need to use it.

In most custom classes, folks would probably not
implement clone() method.
On the other hand, most of the standard classes, part of Java API, such as, say,
all sorts of collections, like Vector, ArrayList, most probably
implement it.
   
You can check that this thing works (I mean, compiles):

    ArrayList al = new ArrayList();
        ArrayList a2 = (ArrayList) al.clone();

   whereas similar operation with your custom written class

     Atest aa = new Atest();    
       Atest bb1 = (Atest) aa.clone();
   
  will fail on compilation unless you override
the clone() method of your Atest class with broader access.

I guess something similar applies also to finalize()

Don't klnow if this thought makes sense, perhaps,
someone else can correct me if this is not right
and there is in fact some other reason for  "protectedness"
of these methods.






















 
 


0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
mohet01Author Commented:
Hello for_yan
With respect to you statement in the above update:
"I can't say it is clear for me, but I can venture a guess.Object was mainly designed for subclassing.
Once you create a new class which is a subclass of Object you will be able to use its clone method only in the process of implementation  of this subclass. Because of the protected nature of say clone() method you'll not be able to use it say in such context "

Even if i override with public access specifier, i simply write super.clone() and access it. I do not write the logic of clone, Because Object class clone() native method is already implemented.
Sham
0
 
for_yanCommented:
Well, I don't think clone() is already implemented for all future classes.
If you create your own object, pouplate it with your own fields and not define how you want to
clone it, but just use super.clone(), I'm afraid your new parameters which you defined will not end up
being populated. I was under the impression that if you want to use cloning of your own objects,
you still have to write code.
 Did you try to do it this way, just relying on the clone() method of Object?
If you look at source code of say ArrayList:

http://kickjava.com/src/java/util/ArrayList.java.htm

you'll see that they implemented clone() not relying on super.clone()

that is how they do it:

public Object  clone() {
 return new ArrayList JavaDoc(this);
   }







0
 
mohet01Author Commented:
Hello for_yan
I understand your point of customsing our own new objects with my version of clone method.
But i would like to say 2 points:
1) Object clone method method is writeen in C for which actually creates as object using some native system calls unlike our version of clone method.
2) I was wondering, when do i keep a method or data member as protected when i design a class , so i took clone() method as example


Sham
0
 
for_yanCommented:

Are you saying that in C they do have a "real" object cloning
method, where they probably do some kind of reflection
analyzing all members and physically copying them
to another location in memory?
That would be really useful, but as I said,
I don't believe we have anything like that in Java.
Still I want to confirm it with some experiments.

I also believe when people use protected in ordinary case (not programming any
system classes) then another property of this attribute - the distinction between in-package
and out-of-package class - becomes maybe even more important than the inheritance stuff.

0
 
mohet01Author Commented:
Yes in C they use system calls to do that
native keyword actually talks about C code
0
 
mohet01Author Commented:
Hello for_yan
Yes, just think when u call super.clone(), unless there is native code how a clone would have been created.
Sham
0
 
mohet01Author Commented:
Make sense
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now