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
174 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java SE 8u111  Lot of stuff broke 11 69
Maven Project: Hibernate Dependencies Conflict 10 24
by zero exception 10 41
jsp login check 12 32
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

803 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