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
170 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
Comment Utility
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
0
 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
Comment Utility

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
 

Author Comment

by:mohet01
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:mohet01
Comment Utility
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
Comment Utility

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
Comment Utility
Yes in C they use system calls to do that
native keyword actually talks about C code
0
 

Author Comment

by:mohet01
Comment Utility
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
Comment Utility
Make sense
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now