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
188 Views
Last Modified: 2012-05-11
Hello
Question is in the title bar
0
Comment
Question by:mohet01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses

628 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