Solved

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?

Posted on 2011-02-12
10
175 Views
Last Modified: 2012-05-11
Hello
Question is in the title bar
0
Comment
Question by:mohet01
  • 5
  • 5
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34881412
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34881424
0
 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
ID: 34882306

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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:mohet01
ID: 34886866
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34890877
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
 

Author Comment

by:mohet01
ID: 34895147
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
 
LVL 47

Expert Comment

by:for_yan
ID: 34895326

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
 

Author Comment

by:mohet01
ID: 34915979
Yes in C they use system calls to do that
native keyword actually talks about C code
0
 

Author Comment

by:mohet01
ID: 34972022
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
 

Author Closing Comment

by:mohet01
ID: 36152264
Make sense
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
why cannot we forward request once the response is committed 2 46
java jdbc batch example 8 41
Unhandled exception type Exception 18 43
learn programming 8 42
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question