wood burning stoves*
The moose likes Java in General and the fly likes inheritance for Object class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "inheritance for Object class " Watch "inheritance for Object class " New topic
Author

inheritance for Object class

Manish Thapliyal
Greenhorn

Joined: Aug 07, 2007
Posts: 17
Please watch the following code, I have a query about it.

Basically there are two classes Test2 & Test3 in the two different packages.

package test1;
public class Test2
{protected int test=8;}


package test3;
import test1.*;
class Test3 extends test1.Test2
{
//static int test5=getTest();
static Test3 t=new Test3();
public static void main(String a[])
{
System.out.println(t.test);
t.toString();
}
}


Now class Test3 extend Test2.As Object is the root class then Test2 has java.lang.Object class as it's superclass.
So Test3 class indirectly inherits the Object Class.

No my query is that :
Object class has only two methods protected remaning are default(no access outside the package.)
Then is that means the class Test3 does not have access the other methods of Object class such as equals(),hashcode.


Manish
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Manish,

the methods in class Object do NOT have default access! They are all public and protected (only clone and finalize)!

Perhaps you only looked at the "method summary" section in the API documentation. There are indeed no access modifiers but in the detailed method descriptions you will see that they're public

Marco
[ June 03, 2008: Message edited by: Marco Ehrentreich ]
Manish Thapliyal
Greenhorn

Joined: Aug 07, 2007
Posts: 17
thanks marco for your reply
you were correct I have seen only the api method summary.

Actually i have reached to this question while searhing why the finallizer() & clone method are protected . why not public or private
Please explain.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

The reason is very simple... The methods clone() and finalize() don't have a very useful default implementation in class Object so they are left protected.

If you need them you can override the methods in your subclass and by overriding you can even promote them to "public" access if you like.

Marco
Manish Thapliyal
Greenhorn

Joined: Aug 07, 2007
Posts: 17
AS I understood, finallize() is protected not public because minimum accesbility for overriding is the protected.

But why finnalize() is not private i am unable to understand?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

You already gave the answer yourself

finalize() and clone() are not public because they are not very useful with their default behavior in class Object.

And they are protected and not private because they are expected to be overridden by subclasses!!! If they were private they couldn't be overridden, that's the point! As you said the minimum access modifier to allow overriding is "protected".

Marco
 
Don't get me started about those stupid light bulbs.
 
subject: inheritance for Object class
 
Similar Threads
Possible error in K&B book (resolved - no error)
forward referencing
protected field
procted variable access in subclass
Inheritance Confusion